Nasrin Naher
Nasrin Naher

Reputation: 87

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

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?

Upvotes: 6

Views: 53471

Answers (6)

Syed Ahamed
Syed Ahamed

Reputation: 1

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

Upvotes: 0

Pinakin Chaubal
Pinakin Chaubal

Reputation: 41

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

Upvotes: 0

Ripon Al Wasim
Ripon Al Wasim

Reputation: 37746

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")));

Upvotes: 0

M Abbas
M Abbas

Reputation: 6479

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

Upvotes: 12

Robbie Wareham
Robbie Wareham

Reputation: 3428

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!

Upvotes: 4

r3ap3r
r3ap3r

Reputation: 2815

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"));

Upvotes: 1

Related Questions