Saturday, September 22, 2018

How To Wait For Page To Load/Ready In Selenium WebDriver

In this tutorial, we are going to explain how to wait for page to load/ready in selenium webdriver. Generally selenium WebDriver handles web page loading or wait for page to load by It self. But sometime web pages are taking more time to load page, due to that driver script unable to find specified element in web page and that results in issue. So in that case selenium webdriver will throws exception message in console and stop the script execution. Here in this example we are using javascript executor to overcome page load issue and javascript executor also provides predefined function, that helps to check page load status.

How To Wait For Page To Load/Ready In Selenium WebDriver

Javascript executor example to check page loading status :

JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("return document.readyState").toString().equals("complete");

Lets see the  complete source code, where we are using javascript executor to check page load status.
  • checkPageIsReady() method checks page load status. if page is loaded  successfully within specified time that is 25 seconds, then it will display message "Page Is loaded." in console.
package demoPackage;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.PageLoadStrategy;
import org.openqa.selenium.WebDriver;

public class CheckPageReady {
 public static WebDriver driver = null;
 public static JavascriptExecutor js;
 public static WebDriverWait wait; 
 public static void main(String args[]){
  System.out.println("Launching the chrome driver  ");
  // Set the chrome driver exe file path
  // Instantiate the chrome driver
  driver = new ChromeDriver();
  driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
  // calling checkPageIsReady() method to check page loaded status.  
  // once page is loaded, it will click on below link.
  driver.findElement(By.cssSelector("#Label1 > div > span:nth-child(19) > a")).click();
 public static void checkPageIsReady() {

  JavascriptExecutor js = (JavascriptExecutor) driver;

  // Initially bellow given if condition will check ready state of page.
  if (js.executeScript("return document.readyState").toString().equals("complete")) {
   System.out.println("Page Is loaded.");

  // This loop will iterate for 25 times to check If page Is ready after
  // every 1 second.
  // If the page loaded successfully, it will terminate the for loop
  for (int i = 0; i < 25; i++) {
   try {
   } catch (InterruptedException e) {

   // To check page ready state.
   if (js.executeScript("return document.readyState").toString().equals("complete")) {

This is all about check or wait for page to load In selenium webdriver. Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

1 comment:

  1. // To check page ready state.
    if (js.executeScript("return document.readyState").toString().equals("complete")) {

    this should be inside the for loop