控制 browser.wait() 的轮询频率(流畅等待)

Controlling poll frequency of browser.wait() (Fluent Wait)

故事:

在 Java selenium 语言绑定中有一个 FluentWait class,它允许严格控制如何检查预期条件:

Each FluentWait instance defines the maximum amount of time to wait for a condition, as well as the frequency with which to check the condition. Furthermore, the user may configure the wait to ignore specific types of exceptions whilst waiting, such as NoSuchElementExceptions when searching for an element on the page.

换句话说,可以更改应用预期条件检查的轮询间隔,默认为 500 毫秒。另外,可以将例外设置为忽略。

also possible in PythonWebDriverWaitclass.

有相关的poll_frequencyignored_exceptions参数

问题:

在Protractor/WebDriverJS中使用browser.wait()时,是否可以控制验证预期条件的轮询频率


根据 browser.wait() documentation,只有 3 个可能的参数:作为预期条件的函数、超时值和可选的超时错误消息。我希望有不同的设置或方法来更改轮询频率。

好吧,根据我阅读的文档,似乎没有反映 FluentWait 功能的实际方法。相反,此代码段可用于更改轮询频率,并且它可以避开几乎所有异常(几乎所有)。

def wait_until(predicate, timeout_seconds, period=0.25, show_log=True):
    """
    @summary Call repeatedly the predicate until it returns true or timeout_seconds passed.
    @param predicate: a condition, modelized as a callable,  that will valued either as True or False
    @param timeout_seconds: the timeout in second
    @param period: the time to sleep between 2 calls to predicate. Defaults to 0.25s.
    @return True if a call to predicate returned True before timeout_seconds passed, else False
    """
    if show_log:
        myLogger.logger.info("waiting until predicate is true for {end} seconds".format(end=timeout_seconds))
    ultimatum = time.time() + timeout_seconds
    while time.time() < ultimatum:
        myLogger.logger.debug("checking predicate for wait until : {spent} / {end}".format(spent=str(time.time() - (ultimatum - timeout_seconds)), end=timeout_seconds))
        try:
            if predicate():
                return True
            time.sleep(period)
        except Exception as e:
            myLogger.logger.warn("Exception found: {}".format(e))
    return False

因此,谓词可以是您传递的 lambda,它验证所讨论的 WebElement 的状态。

在@Kirill S. 的帮助下,在进一步研究和检查 WebdriverJS source code 之后,我可以得出结论 [=] 中没有 "poll frequency" 这样的东西25=] 硒结合。无法配置后续条件检查调用之间的间隔 - 它会尽快执行检查。

这与 Python 或 Java selenium 绑定 中的不同,其中预期条件状态之间存在可配置的超时检查。默认情况下,it would wait for 500ms before the next check:

WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.