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

I am new to Selenium. I am using Selenium WebDriver with Java. I am using eclipse as an IDE. I wrote the code for the login page and it started successfully. Now I want to go to the desired page after a successful login, but I want to wait a little while before going to another page. How can I wait for a page to load another page?

+6
source share
5 answers

As far as I know, there are 3 ways:

  • Implicit wait: (it applies to all elements on the page)

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

  • Explicit Expectation: (applicable for a specific item)

    WebDriverWait.until(CONDITION_THAT_FINDS_AN_ELEMENT);

More specific code is as follows:

 WebDriverWait wait = new WebDriverWait(driver, 40); WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); 
  • Using Thread:

    Thread.sleep(NUMBER_OF_MILLIS);

+12
source

I would advise against using Thread.sleep (NUMBER_OF_MILLS). It will not be stable, and you will find yourself in a situation where the dream will not be long enough.

If you are just waiting for the DOM to load, then the WebDriver event, which triggers the page to load, will always wait for the DOM to load before returning control.

However, if AJAX is used to change HTML after the DOM, I would advise you to use WebDriverWait and wait until a known event occurs (for example, Object appears in html, text changes, etc.)

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

+4
source

Use class WebDriverWait

Selenium Explicit / Implicit Waiting

You can wait until the item you expect on the next page appears.

 WebDriver _driver = new WebDriver(); WebDriverWait _wait = new WebDriverWait(_driver, TimeSpan(0, 1, 0)); _wait.Until(d => d.FindElement(By.Id("Id_Your_UIElement")); 
+1
source

Try using implicitlyWait for 60 seconds. as stated in Selenium WebDriver:

 driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);//wait for 60 sec. 

If you need to wait yet, do it 80, 90, etc.

For Selenium RC, you can use the code as shown below:

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

This can be done using Thread, as shown below:

 Thread.sleep(NUMBER_OF_MILLIS); 

To wait explicitly in WebDriver, identify the item on the download page and write the code as shown below:

 WebDriverWait wait = new WebDriverWait(driver, 40);//wait for 40 sec. WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id("someid"))); 
0
source

I used this approach. Try to figure out which item on the page was last loaded. By taking the locator of this element and checking its existence using isDisplayed, you can see when the whole page loads

0
source

Source: https://habr.com/ru/post/952269/


All Articles