Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
selenium.wait_for_条件在Web驱动程序的Python绑定中是等效的_Python_Selenium_Webdriver - Fatal编程技术网

selenium.wait_for_条件在Web驱动程序的Python绑定中是等效的

selenium.wait_for_条件在Web驱动程序的Python绑定中是等效的,python,selenium,webdriver,Python,Selenium,Webdriver,我正在将一些测试从Selenium转移到WebDriver。我的问题是我找不到硒的等效物。等待条件。Python绑定现在有这样的功能吗,还是仍在计划中 Java绑定包含一个Wait类。这个类反复检查一个条件(在两个睡眠之间),直到达到超时。如果您可以使用普通API检测Javascript的完成情况,则可以采用相同的方法。目前无法对WebDriver使用wait\u for\u条件。python selenium代码确实提供了DrivenSelenium类来访问旧的selenium方法,但它不能等

我正在将一些测试从Selenium转移到WebDriver。我的问题是我找不到硒的等效物。等待条件。Python绑定现在有这样的功能吗,还是仍在计划中

Java绑定包含一个Wait类。这个类反复检查一个条件(在两个睡眠之间),直到达到超时。如果您可以使用普通API检测Javascript的完成情况,则可以采用相同的方法。

目前无法对WebDriver使用wait\u for\u条件。python selenium代码确实提供了DrivenSelenium类来访问旧的selenium方法,但它不能等待条件

您最好使用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
这个解决方案决不是理想的。。对于页面请求/响应周期延迟等待某个ajax活动完成的情况,try/except是必需的。如果在请求/响应周期中调用compare\u source get,它将抛出WebDriverException


查看也很有帮助。

以下是我对格雷格·萨德茨基答案的版本,将其放入函数中:

def click_n_wait(driver, button, timeout=5):
    source = driver.page_source
    button.click()
    def compare_source(driver):
        try:
            return source != driver.page_source
        except WebDriverException:
            pass
    WebDriverWait(driver, timeout).until(compare_source)

它单击按钮,等待DOM更改,然后返回。

似乎可以测试可见性<代码>从selenium.webdriver.support导入预期的条件为ec,然后导入(elm)的ec.visibility。它的返回对象是的selenium.webdriver.support.expected\u conditions.visibility\u,但我还没有弄清楚如何从中获得可见性。