代码之家  ›  专栏  ›  技术社区  ›  Oleh Rybalchenko

Selenium WebDriver等待组合的预期条件(AND、OR、NOT运算符)[重复]

  •  1
  • Oleh Rybalchenko  · 技术社区  · 8 年前

    如何使Selenium WebDriver等待到组合的预期条件?

    基本上,Java的类似问题是 asked answered ,但这种方法 OR ( docs )这里不是用于Python绑定的( expected_conditions.py 在GitHub上)

    我有一个非常缓慢的回调,结果是:

    • 身份证号码= _report_success 如果所有东西都正确加载
    • 身份证号码= _report_error 万一发生故障

    所以,我要等到 _报告成功 _报告错误 变得清晰可见。

    另外,这些条件非常简单:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, '_report_success')))
    
    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.ID, '_report_error')))
    

    在Java中,组合版本如下所示:

    driverWait.until(ExpectedConditions.or(
            ExpectedConditions.presenceOfElementLocated(...),
            ExpectedConditions.presenceOfElementLocated(...)
    ));
    

    当然,我可以做一个循环并用间隔来检查两者的存在(实际上就像在WebDrReValue.to中实现的那样),但是我正在寻找更优雅和灵活的解决方案。毕竟,如果这种需求的方法出现在Java版本中,为什么它不在Python绑定中?

    1 回复  |  直到 8 年前
        1
  •  2
  •   Guy    8 年前

    你可以用 css_selector 对于 OR

    WebDriverWait(driver, 5).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#_report_success, #_report_error')))