Wait for page load in Selenium

  • 5 Replies
  • 2956 Views
Wait for page load in Selenium
« on: March 09, 2016, 11:42:02 am »
How do you make Selenium wait for the web page to load entirely ?

I can't find some elements to run automated tests on.

Re: Wait for page load in Selenium
« Reply #1 on: March 10, 2016, 09:34:28 am »
You could do it by using an explicit wait with the WebDriverWait class method until.

Code
driver.get("http://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
Predicate<WebDriver> predicate = driver1 -> ((JavascriptExecutor)driver1).executeScript("return document.readyState").equals("complete");
wait.until(predicate);
System.out.println("page loaded !");

quote from http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
Quote
This waits up to 10 seconds before throwing a TimeoutException or if it finds the element will return it in 0 - 10 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

*

Oren

  • **
  • 59
Re: Wait for page load in Selenium
« Reply #2 on: March 10, 2016, 10:05:22 am »
By the way, you don't have to wait for the entire page to load if your aim is to automate a single element or two.

You could use the WebDriverWait to check for expected conditions :

You could check for a specific element to be located on the DOM :
Code
WebDriver driver = new FirefoxDriver();
driver.get("http://example.com");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
  .until(ExpectedConditions.presenceOfElementLocated(By.id("id")));


Re: Wait for page load in Selenium
« Reply #3 on: March 10, 2016, 10:13:04 am »
Thanks those are really helpful.

I have an issue with that code you posted Walter.
I'm getting a syntax error on
Code
wait.until(predicate);
: "The method until(com.google.common.base.Predicate<org.openqa.selenium.WebDriver>) in the type FluentWait<WebDriver> is not applicable for the arguments (java.util.function.Predicate<org.openqa.selenium.WebDriver>)"

Re: Wait for page load in Selenium
« Reply #4 on: March 10, 2016, 10:14:36 am »
Did you import com.google.common.base.Predicate or java.util.function.Predicate ?

Re: Wait for page load in Selenium
« Reply #5 on: March 10, 2016, 10:16:20 am »
Oh i see now - i imported from java.util.function by mistake. didn't notice it should be the other one.

Thanks again !

 

Sitemap 1 2 3 4 5 6 7 8