How to wait a page before going to another page in Selenium WebDriver using Java?
Asked Answered
D

6

6

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?

Drawn answered 22/8, 2013 at 10:4 Comment(1)
What are you using? Selenium RC Or Selenium WebDriver?Cruise
S
12

As far as I know, there are 3 ways:

  1. Implicit wait: (It's applicable for all elements on the page)

    driver.manage().timeouts().implicitlyWait(A_GIVEN_NUMBER, TimeUnit.SECONDS);

  2. Explicit wait: (Applicable for a particular element)

    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")));
  1. Using Thread:

    Thread.sleep(NUMBER_OF_MILLIS);

Scholium answered 22/8, 2013 at 10:24 Comment(4)
I used: Thread.sleep(NUMBER_OF_MILLIS); now it's working.Thanks mabbas.Drawn
WebDriverWait is better to wait for an element. In this case, it needs to identify an element of landing page.Cruise
What does the figure 40 stand for in the statement : WebDriverWait wait = new WebDriverWait(driver, 40);Carrew
@TejeshRaut The timeout in seconds when an expectation is calledScholium
A
4

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.

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.

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.)

If you take one thing away from this post, then please stop using Sleep!

Arcane answered 22/8, 2013 at 12:30 Comment(0)
W
1

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"));
Whall answered 22/8, 2013 at 10:25 Comment(1)
this throws unable to locate element exception in the Until method.Comparable
C
0

Try by using implicitlyWait for 60 sec. as follows in 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.

For Selenium RC you can use the code as below:

selenium.waitForPageToLoad("60000");//wait for 60 sec.

It can be done by using Thread as below:

Thread.sleep(NUMBER_OF_MILLIS);

For explicit wait in WebDriver, identify an element in loading page and write the code as below:

WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec.
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid")));
Cruise answered 22/8, 2013 at 11:2 Comment(0)
F
0

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

Funeral answered 5/9, 2017 at 8:40 Comment(0)
D
0

Implicit Wait:

driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

Explicit Wait:

WebDriverWait wait = new WebDriverWait(driver, 40);

This code is deprecated in Selenium 4.

Instead, use this,

driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(60));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(40));
Diathermic answered 5/5, 2022 at 10:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.