I'm trying to find a whatsapp icon "attachment" through the class_name and enter the code below
link = f'https://web.whatsapp.com/send?phone={numero}&text={texto}'
navegador.get(link)
sleep(10)
navegador.find_element(By.CLASS_NAME, 'li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)').send_keys(Keys.ENTER)
I'm doing an automation of sending pdf whatsapp and I want to enter the attachment so I can apply the pdf
When trying to look for the class_name my code does not run looking for that icon or the class
return me
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)
Stacktrace:
RemoteError@chrome://remote/content/shared/RemoteError.sys.mjs:8:8
WebDriverError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:182:5
NoSuchElementError@chrome://remote/content/shared/webdriver/Errors.sys.mjs:394:5
element.find/</<@chrome://remote/content/marionette/element.sys.mjs:280:16
CodePudding user response:
I have no idea if this locator valid li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)
but it definitely not looks like a class name. It looks like a CSS Selector.
So, instead of navegador.find_element(By.CLASS_NAME, 'li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)').send_keys(Keys.ENTER)
try changing it to:
navegador.find_element(By.CSS_SELECTOR, 'li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)').send_keys(Keys.ENTER)
CodePudding user response:
The Class of the object looks different on my end
<div aria-disabled="false" role="button" tabindex="0" data-tab="10" title="Attach" aria-label="Attach">
<span data-testid="clip" data-icon="clip" >...
You might want to consider that the class names and css selectors might change over time and there are other methods.
To me , the div with role="button" looks like a better candidate to go for , since its already the button that when clicked will trigger the action of choosing what to attach, so you could try to use the find by title functionality to get to the button called Attach directly.
You can see how to go about that method here and see if its a better option for your needs.