我正在尝试使用 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/