Need to wait until page is completely loaded - Selenium WebDriver

0 votes
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
• 10,730 points

edited Apr 4, 2018 by nitinrawat895 6,558 views

2 answers to this question.

0 votes

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). 
answered Apr 2, 2018 by nsv999
• 5,110 points
0 votes

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 by Abha
• 27,180 points

Related Questions In Selenium

0 votes
1 answer

Need Selenium to wait until the document is ready.

Use this: driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS); This piece of code will ...READ MORE

answered Apr 20, 2018 in Selenium by king_kenny
• 3,650 points
2,574 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,420 points
6,070 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,130 points
747 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 13, 2018 in Selenium by king_kenny
• 3,650 points
965 views
0 votes
1 answer

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,110 points
983 views
0 votes
1 answer
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,130 points
1,140 views
0 votes
1 answer

I tried to open a new website when a new tab is opened using Selenium Webdriver

Try this code: ArrayList<String> tabs = new ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
1,079 views
0 votes
1 answer