简体   繁体   English

如何使用 javascript 在 selenium 中刷新页面直到元素消失

[英]How to refresh the page until element is disappeared in selenium with javascript

In selenium JavaScript, I have to refresh the page until element is disappeared.在 selenium JavaScript 中,我必须刷新页面直到元素消失。 Since it uses promises, if I use for loop it will create many promise even if the condition is satisfied, later it will be resolved.由于它使用promise,如果我使用for循环它会创建许多promise,即使条件满足,稍后也会解决。 Also I have to provide driver.sleep(5000) mandatory due to some reason.由于某种原因,我还必须强制提供 driver.sleep(5000) 。

$browser.get("https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html").
then( function()
{
var p=Promise.resolve();
for (let index = 0; index < 5; index++) {    
    p = p.then(() => 
    isElementDisplayedAfterrefresh($driver.By.xpath("//span[text()='PouchDB']"), "input"))
    .then(a=>$browser.sleep(3000))
    .then(()=>$browser.navigate().refresh())
}
return p;
}
).then(
    ()=>console.log('done'))

code should exit if element is not displayed, it should refresh if element is still displayed.如果元素没有显示,代码应该退出,如果元素仍然显示,它应该刷新。 How to loop through promise sequentially when we don't know results/number of iterations the return type is Promise, How to break loop?当我们不知道结果/迭代次数返回类型为 Promise 时,如何依次循环通过 promise,如何中断循环? thanks谢谢

Edit编辑

https://gist.github.com/saiparth/045c32267e55b836f139bdac6597e57b Issue is it schedules commands for loop, so all 5 iteration will execute no matter for previous index worked not. https://gist.github.com/saiparth/045c32267e55b836f139bdac6597e57b问题是它为循环安排命令,所以所有 5 次迭代都不会执行之前的索引。 Also I should not use async/await我也不应该使用 async/await

How do you know if the element is displayed or not, seems like you are refreshing the page anyway if I am reading it correctly?你怎么知道元素是否显示,如果我阅读正确,你似乎正在刷新页面? .then(a=>$browser.sleep(3000)) shouldn't it be something like .then(a=>$browser.sleep(3000))不应该是这样的

.then((a => { if(a.isDisplayed() == true) { console.log('element displayed dont do anything') } 
  else { 
     $browser.sleep(3000))
    .then(()=>$browser.navigate().refresh())
}  }))

Finally I found the solution after referring this最后我在参考this后找到了解决方案

function process(index) {
    if (index >= 10) {
        return;
    }
   return $browser.findElements(By.xpath("//span[text()='PouchDB']")).then(a => a.length==0)
        .then(function (isDisplayed) 
        {
            if (!isDisplayed) {
               return $browser.sleep(5000)
                .then(() => $browser.navigate().refresh())
                .then(a=>process(index + 1))
            }  
            else{
               return;
            }
        });
}
$browser.manage().window().maximize();
$browser.get("https://pouchdb.com/2015/05/18/we-have-a-problem-with-promises.html")
.then(a=>process(5))
.then(a=>console.log('done'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM