python - 如何解决此问题元素不可交互的 Selenium Python

标签 python selenium selenium-webdriver selenium-chromedriver

我的脚本代码中有以下行,我从 Selenium IDE 获得的 XPath 运行良好:

driver.find_element_by_xpath("(//a[contains(@href, '')])[20]").click()

自动化测试因以下错误而在此处停止:

Traceback (most recent call last):
File "Script.py", line 65, in <module>
    driver.find_element_by_xpath("//a[contains(@href, '')])[20]").click()
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
File "/usr/lib/python2.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
(Session info: chrome=74.0.3729.131)
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 6.1.7601 SP1 x86_64)

如何解决这个问题?

感谢您的帮助。

最佳答案

鉴于您只想抓取数据,我建议您使用此解决方案:

element = driver.find_element_by_xpath("(//a[contains(@href, '')])[20]")
driver.execute_script("arguments[0].click();", element)

它通过 Javascript 点击元素,而不是 selenium 使用的“自然”点击(试图模拟用户体验)。

我回答了a similar question here也链接到另一篇文章。

关于python - 如何解决此问题元素不可交互的 Selenium Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56194094/

相关文章:

python - 在使用 Flask for Python 3 时如何使用 Bootstrap 4?

java - 等待 Selenium 中的方法返回

angularjs - Protractor 测试没有测试和断言输出,而只有规范

selenium - 通过 Haskell 和 webdriver 重用现有的 Selenium session

java - 从excel中读取测试数据

python - 从 bash 导出变量并在 Python 中使用它

python - 非多态和非抽象基类中的虚析构函数

selenium-webdriver - 如何在 Robot 框架中获取当前 URL?

node.js - Selenium 网络驱动程序 : dragAndDrop method fails in IE11 and IE10

Python 中的 JavaScript CryptoJS HmacSHA256?