Selenium 生成错误“元素不可交互”

新手上路,请多包涵

在此处输入图像描述

我正在尝试使用 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 键,但没有任何效果。

任何见解将不胜感激。

原文由 Daniel Schissler 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 828
2 个回答

在 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")

原文由 Navarasu 发布,翻译遵循 CC BY-SA 4.0 许可协议

对我来说,扩展相对 Xpath 只是帮助了它的父母。

 button = driver.find_element_by_xpath("//button[@data-value='0']")
button.click()
#this did not work

button = driver.find_element_by_xpath("//section[2]/button[@data-value='0']")
button.click()
#this worked well

原文由 Thomasko 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题