首页 > 解决方案 > 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 键,但没有任何效果。

任何见解将不胜感激。

标签: pythonseleniumselenium-webdriverselenium-chromedriver

解决方案


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

btn-primary 类是引导程序中的通用类,将用于所有主按钮。尝试使用唯一定位器,参考模态元素作为定位器中的父级

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

推荐阅读