Need to wait until page is completely loaded - Selenium WebDriver

+1 vote
I'm automating the execution of test cases using Java and Selenium WebDriver. Below is the scenario:

There is a page named 'Products' and When I click on 'View Details' in the 'Product' page, a popup (modal-dialog) containing the details of the items appears.
On clicking the 'Close' button in the popup, the popup closes and the page refreshes automatically (the page is just reloading, the contents remains unchanged).

After closing the popup, I need to click on 'Add Item' button in the same page as part of the test. But, when WebDriver is trying to find the 'Add Item' button, and if the internet speed is good, then webdriver can find and click the element.

But in case the internet speed is bad or slow, then WebDriver finds the button before the page is refreshed, but right when webdriver clicks on the button, the page gets refreshed and StaleElementReferenceException occurs.

Even if different waits are used, all the wait conditions become true (since the contents in the page are the same before and after reloading) even before the page is reloaded and StaleElementReferenceException occurs.
The test case works perfectly if Thread.sleep(); is used before clicking on the 'Add Item' button. But that's not correct usage. Is there any other workaround for this problem?
Apr 2, 2018 in Selenium by nitinrawat895
• 11,380 points

edited Apr 4, 2018 by nitinrawat895 64,089 views

4 answers to this question.

+1 vote

Using Thread.Sleep(); is one approach; but its not the recommended approach.
Theoretically speaking, there are 3 types of wait statements. You can use either Implicit waits, or explicit waits, or fluent waits to wait untill the page reloads. In your case, use implicit waits: driver.manage().timeouts().implicitlyWait() is the command. 

Since you need to wait for the page to be reloaded before clicking on the "Add" button, you need to wait for the "Add Item" element to become stale before clicking on the reloaded element. Usage:

driver.manage().timeouts().implicitlywait(30 timeunit.seconds). 

For further understanding, you can refer to the Selenium Certification.

answered Apr 2, 2018 by nsv999
• 5,500 points
+1 vote

Either you can use Explicit wait for particular web element by following commands: 

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement web1;
web_1= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("web_1")));

Or you can use Implicit wait to let the page load completely:

driver.manage().timeouts().implicitlywait(30 timeunit.seconds) 
answered Aug 27, 2019 by Abha
• 28,140 points
+2 votes

You can try something like - 

new WebDriverWait(firefoxDriver, pageLoadTimeout).until(
      webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
answered Dec 21, 2019 by Robin
0 votes

Thread.sleep() not a good practice.

if it is a page use implicit wait 

driver.manage().timeouts().implicitlywait(30 timeunit.seconds)
any control use explicit wait :
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
WebElement web1;
web_1= wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("web_1")));



answered Sep 4, 2020 by Sri
• 3,190 points

Related Questions In Selenium

0 votes
2 answers

Need Selenium to wait until the document is ready.

use  (JavascriptExecutor) driver).executeScript("return docum ...READ MORE

answered Sep 4, 2020 in Selenium by Sri
• 3,190 points
11,633 views
0 votes
1 answer

To check that the web page is loaded or not using Selenium Web Driver?

The solution is using Implicit Wait which ...READ MORE

answered May 24, 2018 in Selenium by Meci Matt
• 9,460 points
25,078 views
0 votes
1 answer

Wait until page loads before click() using Selenium Webdriver

Change the following guestlogin_button = wait.until(EC.visibility_of_element_located((By.id, ".ch4_loginGuestBtn"))) to guestlogin_button = ...READ MORE

answered Aug 8, 2018 in Selenium by Samarpit
• 5,910 points
4,383 views
0 votes
1 answer

How to check if an image is displayed on web page while working with Selenium WebDriver?

I've come across a similar situation before, where the image ...READ MORE

answered May 10, 2019 in Selenium by Surya
• 970 points
7,822 views
0 votes
1 answer
0 votes
1 answer

Impilicit wait vs Explicit wait vs Fluent wait

Implicit wait: Your telling the WebDriver the ...READ MORE

answered Apr 14, 2018 in Selenium by king_kenny
• 3,710 points
4,281 views
0 votes
2 answers

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,500 points
13,432 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,005 views
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,910 points
7,116 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP