繁体   English   中英

使用 selenium python 单击元素时出现问题

[英]Problem clicking an element with 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()

理想情况下,要单击元素,您需要为element_to_be_clickable()引入WebDriverWait ,您可以使用以下任一定位器策略

  • 使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM