Selenium 多重等待条件失败

Selenium multiple wait conditions fail

以下代码无效。 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()

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

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"))