python - 使用 selenium python 单击元素时出现问题
问题描述
我正在尝试使用 selenium 和 python 单击网页上的元素
driver.find_element_by_class_name("market-selection.ng-scope").click()
但我得到元素不可点击的错误
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
这是元素 html(它不在框架中);我猜可交互的部分是第二个 div 但我也尝试了其他两个以防万一......
<div class="market-selection-container 18" ng-repeat="market in wrapperCategoryGroup.currentMacroCategoria.mkl track by $index">
<!-- ngIf: wrapperCategoryGroup.marketTypes[market].nm -->
<div class="market-selection ng-scope" ng-if="wrapperCategoryGroup.marketTypes[market].nm" ng-class="{'active':wrapperCategoryGroup.currentMarketType == market}" ng-click="wrapperCategoryGroup.getMarketType(market)" style="">
<span class="ng-binding">
doppia chance
</span>
</div>
<!-- end ngIf: wrapperCategoryGroup.marketTypes[market].nm -->
</div>
有什么提示吗?
解决方案
要单击带有文本保存的元素,您可以使用以下任一定位器策略:
使用
css_selector
:driver.find_element_by_css_selector("div.market-selection.ng-scope > span.ng-binding").click()
使用
xpath
:driver.find_element_by_xpath("//div[@class='market-selection ng-scope']/span[@class='ng-binding' and contains(., 'doppia chance')]").click()
理想情况下,要单击需要诱导WebDriverWait的元素element_to_be_clickable()
,您可以使用以下任一Locator Strategies:
使用
CSS_SELECTOR
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.market-selection.ng-scope > span.ng-binding"))).click()
使用
XPATH
:WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='market-selection ng-scope']/span[@class='ng-binding' and contains(., 'doppia chance')]"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
推荐阅读
- angular - 如何通过某些语句(例如通过输入值)投影组件的内容
- vhdl - VHDL:即使在架构声明部分定义了内部信号也是未定义的
- spring - Spring,当我运行我的项目时,它被重定向到登录页面
- python - 如何使用 seaborn relplot 从多个数据帧中绘制数据
- amazon-web-services - 如果云形成条件
- c++ - 有没有办法这个 lambda 可以捕获指针的副本?
- javascript - 对象过滤和链接的JS动态数组
- python - Pyspark 中的 Pandas Udf 在纱线客户端或集群模式下仅在 1 个执行程序中运行
- docker - 来自其他实例和本地 PC 的 docker swarm join 连接错误
- python - 如何在 Python 中从对象中获取数据