如何在使用 Java 进入 Selenium WebDriver 中的另一个页面之前等待一个页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18377384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to wait a page before going to another page in Selenium WebDriver using Java?
提问by Nasrin Naher
I am new in Selenium. I am using Selenium WebDriver with Java. I'm using eclipse as IDE. I have written some code for Login page and it is run successfully. Now I want to go to desired page after successful login, but I want to wait for few time before transiting another page. How can I wait a page before loading another page?
我是硒的新手。我在 Java 中使用 Selenium WebDriver。我使用 Eclipse 作为 IDE。我已经为登录页面编写了一些代码并且它运行成功。现在我想在成功登录后转到所需的页面,但我想在转换另一个页面之前等待一些时间。如何在加载另一个页面之前等待一个页面?
采纳答案by M. Abbas
As far as I know, there are 3 ways:
据我所知,有3种方式:
Implicit wait: (It's applicable for all elements on the page)
driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);
Explicit wait: (Applicable for a particular element)
WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);
隐式等待:(适用于页面上的所有元素)
driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);
显式等待:(适用于特定元素)
WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);
More specific code is as below:
更具体的代码如下:
WebDriverWait wait = new WebDriverWait(driver, 40);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Using Thread:
Thread.sleep(NUMBER_OF_MILLIS);
使用线程:
Thread.sleep(NUMBER_OF_MILLIS);
回答by r3ap3r
Use class WebDriverWait
Selenium explicit / implicit wait
You can wait until the element you are expecting on the next page comes up. :
您可以等到下一页上出现您期望的元素。:
WebDriver _driver = new WebDriver();
WebDriverWait _wait = new WebDriverWait(_driver, TimeSpan(0, 1, 0));
_wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement"));
回答by Ripon Al Wasim
Try by using implicitlyWait for 60 sec. as follows in Selenium WebDriver:
尝试使用隐式等待 60 秒。在 Selenium WebDriver 中如下所示:
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//wait for 60 sec.
If you need to wait more make it 80, 90 and so on.
如果您需要等待更多,则将其设为 80、90 等。
For Selenium RC you can use the code as below:
对于 Selenium RC,您可以使用以下代码:
selenium.waitForPageToLoad("60000");//wait for 60 sec.
It can be done by using Thread as below:
它可以通过使用 Thread 来完成,如下所示:
Thread.sleep(NUMBER_OF_MILLIS);
For explicit wait in WebDriver, identify an element in loading page and write the code as below:
对于 WebDriver 中的显式等待,在加载页面中识别一个元素并编写如下代码:
WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
回答by Robbie Wareham
I really would advise against using Thread.sleep(NUMBER_OF_MILLS). It will not be stable and you will hit occasions when the sleep is not long enough.
我真的建议不要使用 Thread.sleep(NUMBER_OF_MILLS)。它不会稳定,你会遇到睡眠时间不够长的情况。
If you are simply waiting for the DOM to load, then a WebDriver event which triggers page load will always wait for the DOM to load before returning control.
如果您只是在等待 DOM 加载,那么触发页面加载的 WebDriver 事件将始终等待 DOM 加载,然后再返回控制权。
However, if AJAX is used to change the HTML after DOM, then I would advise you to use WebDriverWait, and wait until a known event happens (e.g. Object appears in html, text changes, etc.)
但是,如果使用 AJAX 来更改 DOM 之后的 HTML,那么我建议您使用 WebDriverWait,并等待发生已知事件(例如,对象出现在 html 中,文本更改等)
If you take one thing away from this post, then please stop using Sleep!
如果您从这篇文章中删除了一件事,那么请停止使用睡眠!
回答by Pinakin Chaubal
I have used this approach. Try to figure out which element on the page is the last to load. By taking the locator of that element and checking it's existance using isDisplayed, you will be able to see when the entire page loads
我已经使用了这种方法。尝试找出页面上的哪个元素是最后加载的。通过获取该元素的定位器并使用 isDisplayed 检查它的存在,您将能够看到整个页面何时加载