selenium.wait_for_condition等效于WebDriver的Python绑定

hwi*_*ers 8 python selenium webdriver

我正在将一些测试从Selenium转移到WebDriver.我的问题是我找不到selenium.wait_for_condition的等价物.Python绑定目前是否具有此功能,还是仍在计划中?

sch*_*ckm 11

目前,无法在WebDriver中使用wait_for_condition.python selenium代码确实提供了用于访问旧的selenium方法的DrivenSelenium类,但它不能执行wait_for_condition. 硒维基有一些信息.

最好的办法是使用WebDriverWait类.这是一个辅助类,它定期执行一个等待它返回True的函数.我的一般用法是

driver = webdriver.Firefox()
driver.get('http://example.com')
add = driver.find_element_by_id("ajax_button")
add.click()
source = driver.page_source

def compare_source(driver):
    try:
        return source != driver.page_source
    except WebDriverException:
        pass

WebDriverWait(driver, 5).until(compare_source)
# and now do some assertions
Run Code Online (Sandbox Code Playgroud)

这个解决方案绝不是理想的.对于页面请求/响应周期延迟等待某些ajax活动完成的情况,try/except是必需的.如果在请求/响应周期中调用compare_source,则会抛出WebDriverException.

WebDriverWait测试覆盖率也有助于查看.