Selenium WebDriver JS - 显式等待 [英] Selenium WebDriver JS - Explicit Wait

查看:148
本文介绍了Selenium WebDriver JS - 显式等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用selenium-webdriverjs。我想等待某个元素显示,我已经创建了一个明确的等待,如下所示,它工作得很好,

I am using the selenium-webdriverjs. I want to wait for a certain element to be displayed for which I have created an explicit wait as follows and it works just fine,

var displayed = false;
driver.wait(function(){
    driver.findElement(locator).isDisplayed().then(function(value){
        displayed = value;
    });
    return displayed;
}, timeout);

这是我能做的最好还是有更好的方法吗?我问的原因是第一次调用等待回调(在我的情况下)它将始终返回false。只有在执行isDisplayed promise时,才会显示更改的值。

Is this the best I can do or is there a better way to do this? The reason I ask is that the first time ever the wait callback is called (in my case) it will always return false. Only subsequently when the isDisplayed promise is executed will the value of displayed change.

推荐答案

您的代码将同步返回与异步回调混合,以下代码应该做正确的事情:

Your code is mixing a synchronous return with asynchronous callbacks, the following code should do the right thing:

return driver.wait(function() {
    return driver.findElement(locator).isDisplayed();
}, timeout);

内部函数将返回 driver.wait 将等待并将其值(true / false)作为等待条件。

The inner function will return a promise that driver.wait will wait for and will take its value (true/false) as the waiting condition.

这篇关于Selenium WebDriver JS - 显式等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆