Selenium 元素在问题点上不可点击 - Python

Pyt*_*_09 1 selenium selenium-webdriver

我正在使用 selenium 在 python 中编写一个基本的自动化测试。我可以浏览多个页面,但是当我到达这一特定页面时,我无法单击该按钮。

我的测试失败的代码

driver.find_element_by_id('//*[@id="save"]').click()
Run Code Online (Sandbox Code Playgroud)

当我检查我试图单击的按钮时的元素

<input type="submit" value="View Report" id="save" name="save" data-reportid="108">
Run Code Online (Sandbox Code Playgroud)

下面的错误消息

selenium.common.exceptions.ElementClickInterceptedException:消息:元素单击被拦截:元素在点(1750、770)处不可单击。其他元素将收到点击:

...(会话信息:chrome=83.0.4103.116)

Pst*_*str 9

我有同样的问题。Selenium 没有单击我需要单击的文本字段并引发相同的ElementClickInterceptedException.

对我有用的是改变方法:

该元素就在那里。它已经加载了。我通过使用(在我的示例中)来确保这一点:

WebDriverWait(driver, 60).until(EC.element_to_be_clickable((By.ID, "code")))
Run Code Online (Sandbox Code Playgroud)

好的,那么问题是 Selenium 无法点击它。我们可以使用不同的策略:使用 JS!

使用上述方法使元素可单击后.until(),请记住使用上面的方法将该字段存储在变量中:

button = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='save' and @name='save'][@value='View Report']")))` 
Run Code Online (Sandbox Code Playgroud)

(无需调用.click()

或者通过找到它:

button = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']")
Run Code Online (Sandbox Code Playgroud)

然后是黄金部分:通过JS点击它:

driver.execute_script('arguments[0].click()', button)
Run Code Online (Sandbox Code Playgroud)

为我工作,看看是否对你有帮助


归档时间:

查看次数:

9391 次

最近记录:

3 年,10 月 前