TestProject Forum
Powered by leading experts in the test automation community

Wait for page load in Selenium

How do you make Selenium wait for the web page to load entirely ?

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

You could do it by using an explicit wait with the WebDriverWait class method until.

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

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 :

WebDriver driver = new FirefoxDriver(); driver.get("http://example.com"); WebElement myDynamicElement = (new WebDriverWait(driver, 10)) .until(ExpectedConditions.presenceOfElementLocated(By.id("id")));

Thanks those are really helpful.

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

Did you import com.google.common.base.Predicate or java.util.function.Predicate ?

Oh i see now - i imported from java.util.function by mistake. didn’t notice it should be the other one.

Thanks again !