【问题标题】:Click on item using Selenium and Python使用 Selenium 和 Python 单击项目
【发布时间】:2022-01-07 23:13:48
【问题描述】:

我尝试在 Python 中使用以下代码点击“培训材料统计”,但没有成功:

WebDriverWait(driver,20)\
    .until(EC.element_to_be_clickable((By.XPATH,'//*[@id="report-navigation"]/div[2]')))\
    .click()

HTML:

<div id="report-navigation">
    <div class="report-nav-btn active" onclick="Report.changeGrid(this, 'report-users-grid')">
       User statistics          
       <div class="report-nav-arrow active"></div>
    </div>
    <div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-objects-grid')">
       Training material statistics         
       <div class="report-nav-arrow"></div>
    </div>
    <div class="report-nav-btn" onclick="Report.changeGrid(this, 'report-deliverables-grid')">
       Learner assignments          
       <div class="report-nav-arrow"></div>
    </div>
</div>

HTML 快照:

【问题讨论】:

  • HTML?代码?错误信息?您需要向我们提供更多信息。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

点击元素需要诱导WebDriverWaitelement_to_be_clickable(),可以使用以下Locator Strategies之一:

  • 使用CSS_SELECTOR

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div#report-navigation div[onclick*='report-objects-grid']"))).click()
    
  • 使用XPATH

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@id='report-navigation']//div[contains(@onclick, 'report-objects-grid')]"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    猜你喜欢
    • 2021-09-08
    • 2020-05-26
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2018-08-22
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    相关资源
    最近更新 更多