python - 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 键,但没有任何效果。
任何见解将不胜感激。
解决方案
在 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")
推荐阅读
- autohotkey - 为什么热字符串会出人意料?
- web-services - 没有布局的 Odoo v9 Webservice 报告
- odoo - 我可以更改 Odoo 中的“员工”标签吗?
- javascript - 在 unsubscribe 函数中异步处理资源
- php - 需要使用 postfix 减少响应时间
- javascript - 在 Node 中关闭 MSSQL 连接
- shortcut - 无效的 Siri 支持 - 自定义意图的本地化描述
- visual-studio-code - 将终端移回底部
- android - 使用 Dagger 2 在计划的 JobService 中进行依赖注入
- javascript - TypeError 的原因:对严格模式调用函数的访问被审查