【问题标题】:Selenium multiple wait conditions failSelenium 多个等待条件失败
【发布时间】:2021-10-22 16:59:38
【问题描述】:

以下代码不起作用。 selenium web 驱动程序无需等待即可继续执行,即使页面上的任何元素都不可见。因此断言失败。

element = WebDriverWait(self.driver, 30).until(
            lambda x: (EC.visibility_of_element_located((By.ID, "export_errors_button"))) or
                      (EC.visibility_of_element_located((By.ID, "finish_button")))
        )
assert "finish_button" in element.get_attribute('id').split()

【问题讨论】:

  • 表示你的ids确实存在于页面上,因此它不会等待,没有暂停之类的东西,不推荐使用thread.sleep,所以我有一个小技巧使用其实就是等待一个不存在的元素并捕获异常
  • 请您详细说明一下。我也在检查它们的可见性,而不是它们的存在。
  • EC.presence_of_element_located() 产生相同的结果?
  • 在 HTML DOM 中可见吗? Selenium 与 HTMLDOM 的交互与您在 UI 中看到的不同。
  • EC.presence_of_element_located() 产生相同的结果。两个元素都设置为 style="display:none"。

标签: selenium selenium-webdriver implicitwait


【解决方案1】:

我最终编写了一个自定义等待条件。

def one_of_these_elements_is_visible(element1, element2):
    def wait_for_condition(driver):
        attribute = driver.find_element(By.ID, element1).is_displayed()
        if attribute:
            return True
        else:
            return driver.find_element(By.ID, element2).is_displayed()
    return wait_for_condition


element = WebDriverWait(self.driver, 20).until(one_of_these_elements_is_visible("finish_button",
                                                                              "export_errors_button"))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-25
    • 2018-09-18
    • 2020-10-30
    • 1970-01-01
    • 2019-04-20
    • 2019-10-16
    • 2023-03-03
    • 2022-01-21
    相关资源
    最近更新 更多