Selenium Webdriver 2.47.1如何等待页面重新加载 [英] Selenium webdriver 2.47.1 how to wait for a page reload

查看:229
本文介绍了Selenium Webdriver 2.47.1如何等待页面重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用webdriver(java)进行独特的测试,登录时页面会自动重新加载,我一直无法等待元素加载,因为元素在重新加载之前就已经存在.到目前为止,我能够使其正常工作的唯一方法是使用thread.sleep,有没有办法监听页面刷新?

I'm using webdriver(java) for a unique test where a page reloads itself when you log on, I've been unable to wait for the element to load because it is already there before the reload. So far the only way I've been able to get it to work is to use a thread.sleep, is there a way to listen for a page refresh?

推荐答案

一种解决方法是,获取对所需元素的引用,该元素同时出现在登录页面和重新加载的页面上. 然后,可以使用ExpectedConditions.stalenessOf进行操作,然后可以确保从DOM中删除了该元素,并创建了一个新元素.好吧,这种方法不能保证最后一部分,但是至少您知道旧元素已经消失了.

One way to solve this is, to get a reference to the element you need, that appears both on the login-page and the reloaded page. Then you can use the ExpectedConditions.stalenessOf to occur, and then you can be sure, that the element is removed from the DOM and a new element is created. Well, the last part is not guaranteed by this method, but at least you know that the old element is gone.

代码可能看起来像这样:

The code could look something like this:

WebElement elementOldPage = driver.findElement(By.id("yourid"));

... do login etc ...

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.stalenessOf(elementOldPage));

WebElement elementNewPage = driver.findElement(By.id("yourid"));

这篇关于Selenium Webdriver 2.47.1如何等待页面重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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