python - Selenium 生成错误 "Element is not interactable"

标签 python selenium selenium-webdriver selenium-chromedriver

enter image description here

我正在尝试使用 Selenium 单击上面突出显示的按钮。我可以通过以下方式定位元素:

download_button_path = "//button[@class='btn-primary']"
download_button = driver.find_element_by_xpath(download_button_path)

但是当我尝试执行时:

download_button.click()

我收到错误信息:

ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=70.0.3538.67)
  (Driver info: chromedriver=2.42.591059 (a3d9684d10d61aa0c45f6723b327283be1ebaad8),platform=Mac OS X 10.11.6 x86_64)

似乎该按钮对 selenium 不可见,即使我在手动执行单击时能够看到它。

我也尝试过将鼠标悬停在按钮上然后单击,以及向按钮发送 Enter/Return 键,但没有任何效果。

如有任何见解,我们将不胜感激。

最佳答案

在 HTML 中,我看到 btn-primary 出现在引导模式弹出窗口中。所以在 modal pop 后面可能还有另一个 btn-primary。 XPath 将找到不可交互的模式后面的元素。

btn-primary 类是 bootstrap 中的通用类,将在所有主按钮中使用。尝试使用唯一定位器,并在定位器中将模态元素作为父元素进行引用

download_button_path = "//[@class='lmn-edititem-modal']/../[@class=''btn-primary']"
wait = WebDriverWait(driver, 10)
download_button = wait.until(EC.visibility_of_element_located((By.XPATH, download_button_path)))
download_button .click()

我们也可以用 CSS 选择器试试这个

driver.find_elements_by_css_selector(".lmn-edititem-modal .btn-primary") 

关于python - Selenium 生成错误 "Element is not interactable",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52859981/

相关文章:

python - 如何按类别和/或位置解析 Facebook 页面的 FQL 响应? (Python)

java - 无法使用 Chrome Web 驱动程序访问 servlet

java - 如何在不添加 IE 驱动程序或 Chrome 驱动程序的情况下启动已安装的浏览器?

java - 如何在超时后杀死 Selenium "Click"线程

python - 在 Matplotlib 中绘制日期和时间间隔

python - 从 admin inline 的 modelform 访问父模型实例

python - Python中变量的内存大小

java - 使用带 Selenium 的预打开窗口

java - 如何使用 WebDriver 检查元素是否可见

webdriver - 从输入字段中提取文本 - 使用 Webdriver