异常解读 Message: element click intercepted
异常错误信息为:Message: element click intercepted: Element <button … is not clickable at point (835, 33). Other element would receive the click:
…
(Session info: chrome=94.0.4606.81)
该错误是在使用 selenium 的时候,元素被其它内容捕获到了,进而可以分析出 BUG 原因,是由于要点击的元素点击不到。
异常解决方案
- 可以参考下述流程进行分析
- 查看是否是待点击元素写错
- 查看是否有遮罩层
- 查看元素是否被隐藏或移除
具体代码不同,大家自行调整。
如果上述办法都没有解决,可以尝试在 selenium 中执行 JS 代码,进行元素的事件操作。
当然还可以尝试 ActionChains
类与 send_keys()
发送回车键。
附录
本系列文章只供记录 Python 日常开发过程中 偶遇 BUG,提供给学生作为参考数据与解决方案用,属于记录型博客,有缘分看到的读者希望可解决你的问题。
错误提示样板
下述内容,可以与你的错误进行比对。
Traceback (most recent call last):
File "E:/pythonProject/测试/d5.py", line 21, in <module>
login_btn.click()
File "E:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
self._execute(Command.CLICK_ELEMENT)
File "E:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
return self._parent.execute(command, params)
File "E:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "E:\pythonProject\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button class="widget-button btn btn-primary btn-small login-button btn-icon-text">...</button> is not clickable at point (835, 33). Other element would receive the click: <div class="modal-middle-container">...</div>
(Session info: chrome=94.0.4606.81)
补充一些其他解决方案
selenium.common.exceptions.ElementClickInterceptedException
是 Selenium 中的一个常见异常,通常表示在点击元素时发生了拦截。这意味着在点击元素时,另一个元素挡住了它,导致点击无效。
解决这个问题的方法有以下几种
- 使用 JavaScript 模拟点击:使用
driver.execute_script()
方法可以执行 JavaScript 代码来点击元素。 - 等待元素可见:使用 Selenium 的显式等待,等待元素可见后再点击。
- 滚动到元素位置:使用
driver.execute_script()
方法滚动到元素的位置,这样就可以避免元素被遮挡。 - 使用 ActionChains: Selenium 提供的 ActionChains 可以模拟鼠标操作,如果点击不了元素,可以使用 move_to_element() 先移动到元素上。
- 等待元素变化:使用 Selenium 的隐式等待,等待元素发生变化后再点击。