使用Python Selenium获取CLASS_NAME文本



我想找一个whatsapp图标附件通过class_name并输入

下面的代码
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)

我正在做一个自动发送pdf的whatsapp,我想输入附件,这样我就可以应用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

我不知道这个定位器是否有效li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1),但它绝对不像一个类名。它看起来像一个CSS选择器。
因此,将navegador.find_element(By.CLASS_NAME, 'li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)').send_keys(Keys.ENTER)改为:

navegador.find_element(By.CSS_SELECTOR, 'li._2qR8G:nth-child(4) > button:nth-child(1) > span:nth-child(1)').send_keys(Keys.ENTER)

对象的class在我这端看起来不一样

<div aria-disabled="false" role="button" tabindex="0" class="_26lC3" data-tab="10" title="Attach" aria-label="Attach">
<span data-testid="clip" data-icon="clip" class="">...

你可能要考虑到类名和css选择器可能会随时间而改变,而且还有其他方法。

对于我来说,div with role="button"看起来是一个更好的选择,因为它已经是按钮,当点击时会触发选择要附加的内容,所以你可以尝试使用查找标题功能来访问名为attach的按钮。直接

你可以在这里看到如何使用这个方法,看看它是否更适合你的需要。

相关内容

最新更新