【发布时间】:2021-11-18 12:37:35
【问题描述】:
This is the website我正在尝试自动化一些点击:
我曾尝试使用Xpath 和FullXpath 单击按钮,但还是没有成功。
这是简单的代码:
w = webdriver.Chrome(executable_path='chromedriver.exe',
chrome_options=options)
w.get("https://quillbot.com/")
time.sleep(5)
pasteXpath = "//button[contains(@class,'outlinedPrimary') and .//span[contains(text(),'Paste Text')]]"
element = w.find_element_by_xpath(pasteXpath).click()
但它在控制台中显示此消息失败:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="inOutContainer"]/div[2]/div[2]/div/div[1]/div/div/div[1]/div/div/div[2]/div/div/button/span[1]/div"}
请告诉我如何使用 selenium 自动执行此点击。
【问题讨论】:
-
您的 xpath 是否在相关页面的浏览器中工作?
-
在尝试获取元素之前尝试睡眠几秒钟
-
只是为了进行完整性检查,请尝试对
@id='inOutContainer'使用外部双引号和内部简单引号。或者将整个 XPath 放在三引号内"""并保留内部双引号 -
确保元素已经加载到页面中也很重要。您可以使用
selenium.webdriver.support.wait.WebDriverWait和selenium.webdriver.support.expected_conditions非常简洁易读地做到这一点 -
另外,对我来说,访问您提供的链接的页面看起来不像图像中的那个。首先,我无法访问“扩展”选项,它已被禁用;其次,一个
Try Sample Text按钮显示在Paste Text的位置,虽然奇怪的是后者在重新加载页面时出现了一会儿,就在前者旁边