Selenium WebDriver JS

编程入门 行业动态 更新时间:2024-10-27 22:19:17
本文介绍了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 承诺时,display 的值才会改变.

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 将等待的承诺,并将其值(真/假)作为等待条件.

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

本文发布于:2023-11-24 19:14:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626482.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Selenium   WebDriver   JS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!