【问题标题】:Selenium Cannot Locate a ClassSelenium 找不到类
【发布时间】:2020-04-22 16:22:45
【问题描述】:

所以我在使用 selenium 时遇到了定位类的问题,我已经尽我所能成功定位类属性并使用它执行某些操作,例如:

driver.find_element_by_tag_name('div button')
driver.web.find_element_by_class_name('btn-secondary-md save-button ng-binding')

代码示例:

<button class="btn-secondary-md save-button ng-binding" ng-click="$ctrl.showChangeOwnerModal()" ng-bind="'Label.ChangeOwner' | translate">Change Owner</button>

【问题讨论】:

    标签: python selenium html-parsing


    【解决方案1】:

    尝试通过css选择器定位:

    driver.find_element_by_css_selector('button.btn-secondary-md.save-button.ng-binding')
    

    如果您仍然遇到一些错误,例如元素不可点击,请尝试在 web 元素上使用显式等待。

    【讨论】:

      【解决方案2】:

      您很可能因为同步而面临问题,您可以随时使用 WebDriverWait 来避免同步问题。

      解决方案 1:

      wait = WebDriverWait(driver, 10)
      wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn-secondary-md save-button ng-binding")))
      

      解决方案 2:

      wait = WebDriverWait(driver,30)
      element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Change Owner')]")))
      

      注意:请在您的解决方案中添加以下导入

      from selenium.webdriver.support import expected_conditions as EC
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support.ui import WebDriverWait
      

      如果您仍然遇到问题,请检查您的元素是否在 iframe 中,如果是,则必须将控件切换到 iframe。

      【讨论】:

        猜你喜欢
        • 2021-02-11
        • 2018-09-21
        • 2020-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-16
        相关资源
        最近更新 更多