WebDriver 的 Python 绑定中的 selenium.wait_for_condition 等效项 [英] selenium.wait_for_condition equivalent in Python bindings for WebDriver
问题描述
我正在将一些测试从 Selenium 转移到 WebDriver.我的问题是我找不到 selenium.wait_for_condition 的等价物.Python 绑定目前是否有这个,还是仍在计划中?
I'm moving some tests from Selenium to the WebDriver. My problem is that I can't find an equivalent for selenium.wait_for_condition. Do the Python bindings have this at the moment, or is it still planned?
推荐答案
目前无法将 wait_for_condition 与 WebDriver 一起使用.python selenium 代码确实提供了 DrivenSelenium 类来访问旧的 selenium 方法,但它不能做 wait_for_condition.selenium wiki 提供了一些相关信息.
Currently it isn't possible to use wait_for_condition with WebDriver. The python selenium code does provide the DrivenSelenium class for accessing the old selenium methods, but it can't do wait_for_condition. The selenium wiki has some info on that.
最好的办法是使用 WebDriverWait 类.这是一个辅助类,它定期执行一个函数,等待它返回 True.我的一般用法是
Your best bet is to use the WebDriverWait class. This is a helper class that periodically executes a function waiting for it to return True. My general usage is
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
这个解决方案绝不是理想的.. try/except 对于页面请求/响应周期延迟等待某些 ajax 活动完成的情况是必要的.如果在请求/响应周期中调用 compare_source get,它将抛出 WebDriverException.
This solution is by no means ideal.. The try/except is necessary for situations where the page request/response cycle is delayed waiting for some ajax activity to complete. If compare_source get's called in the midst of the request/response cycle it'll throw a WebDriverException.
WebDriverWait 的测试范围看看也很有帮助.
这篇关于WebDriver 的 Python 绑定中的 selenium.wait_for_condition 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!