【问题标题】:Wait for and then click an item?等待然后单击一个项目?
【发布时间】:2019-09-17 13:10:02
【问题描述】:

所以我想等待一个弹出窗口,然后单击接受。 HTML 看起来像这样:

<div class="termsBtn" onclick="checkTerms(0)" style="background-color:#dd4a42">Decline</div>

<div class="termsBtn" onclick="checkTerms(1)" style="background-color:#a6dd42">Accept</div>


我已经尝试了各种方法,但是对于当前的代码,我得到了 TimeoutException:

selenium.common.exceptions.TimeoutException: Message: 



我当前的代码:

from selenium.webdriver.support import expected_conditions as ec


wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//input[@onclick="checkTerms(1)"]')))
popup.click()

【问题讨论】:

  • 请发布完整的异常消息。

标签: python selenium xpath css-selectors webdriverwait


【解决方案1】:

你的元素没有输入标签,它是一个div标签。在xpath下面试试。

wait = WebDriverWait(driver, 10)
popup = wait.until(ec.element_to_be_clickable((By.XPATH, '//div[@class="termsBtn"][text()="Accept"]')))
popup.click()

【讨论】:

    【解决方案2】:

    你可以使用ExpectedConditions.visibilityOfElementLocated,它会等到元素不可见

    来源:

    Webdriver How to wait until the element is clickable in webdriver C#

    【讨论】:

      【解决方案3】:

      由于所需元素位于弹出窗口中,因此您必须为 element_to_be_clickable() 诱导 WebDriverWait 元素上的 click(),您可以使用以下任一解决方案:

      • 使用CSS_SELECTOR

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.termsBtn[onclick*='1']"))).click()
        
      • 使用XPATH

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='termsBtn' and text()='Accept']"))).click()
        
      • 注意:您必须添加以下导入:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 2023-02-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多