Selenium-8 || Implementing Wait(s) in Selenium.

Wait(s) plays an important role in the Selenium while test execution. Implementing Wait(s) in Selenium is a very interesting and logical concept.

Implicit Wait And Explicit Wait

Need of Wait(s) in Selenium

If we take general definition of wait, it pauses a process for a predefined time or a condition.

Types of Waits

Selenium supports three types of Wait(s) as mentioned below:

  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

Implicit and Explicit waits are the most commonly used in programming. So in this blog, we will learn only these two Waits. Will discuss the FluentWait in later chapters.

Usage and need of Waits

  • Pause the execution till a specific time
  • To implement or check a condition on any element
  • Restart the execution after a specific time or condition
  • execute any code if only an element is visible or clickable
  • Handle loading time in different browsers
  • Most useful and relevant concept over Thread.sleep() method

Implicit Wait

This is a single line statement, which sets a time interval during which WebDriver object waits till an html component loads on the screen. This wait would execute after each WebDriver statement automatically. Consider the below code without any wait():

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author ashok.kumar
 */
public class ImplcitWaitExample {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
		WebDriver driver=new ChromeDriver();
		driver.get("http://allinoneblogs.com");
		
		String closeXpath="//*[@id=\"popup_box_close_1484\"]";
		driver.findElement(By.xpath(closeXpath)).click();
		
		String postLinkXpath="//section[@class='latest-post-selection']/article[1]/a/h3";
		driver.findElement(By.xpath(postLinkXpath)).click();		
	}
}

The above code will throw an exception “Exception in thread “main” org.openqa.selenium.ElementNotVisibleException: element not interactable” for below statement : -driver.findElement(By.xpath(postLinkXpath)).click();

Implementing Implicit Wait

We just need to add a single line statement after launching the web browser.

driver.manage().timeouts().implicitlyWait(<time>,TimeUnit);

  • <time> is an integer value of predefined duration.
  • TimeUnit can be any unit of time like SECONDS, MILLISECONDS, HOURS, DAYS, Minutes, etc.

Below is the updated code for the same. But we need to take care that if the component doesn’t get loaded within the specified time compiler will throw the same exception which is mentioned earlier. So we need to adjust the time accordingly.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

/**
 * @author ashok.kumar
 */
public class ImplcitWaitExample {
	public static void main(String[] args){
		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
		
		WebDriver driver=new ChromeDriver();
		driver.get("http://allinoneblogs.com");
		
		
		  // Implementing Implicit Wait 
	driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
		 
		
		String closeXpath="//*[@id=\"popup_box_close_1484\"]";
		driver.findElement(By.xpath(closeXpath)).click();
		
		String postLinkXpath="//section[@class='latest-post-selection']/article[1]/a/h3";
		driver.findElement(By.xpath(postLinkXpath)).click();		
	}
}

Explicit Wait

As we already know that Implicit Wait, sets the same time globally and for each WebDriver statement till element gets loaded on the screen. But sometimes we need to set wait for a specific statement only. Explicit wait tells the WebDriver to wait till a specific condition get fulfilled or throwing an exception after a specified time in the Explicit Wait statement.

We could implement Explicit Wait, in the above program by replacing the statement used for Implicit Wait. Below is the updated code to demonstrate the same.

import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

/**
 * @author ashok.kumar
 *
 */
public class ExplictWaitExample {
	public static void main(String[] args)
	{
		System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
		
		WebDriver driver=new ChromeDriver();
		driver.get("http://allinoneblogs.com");
		
		String closeXpath="//*[@id=\"popup_box_close_1484\"]";
		driver.findElement(By.xpath(closeXpath)).click();
		
		String postLinkXpath="//section[@class='latest-post-selection']/article[1]/a/h3";
		
// Implement Explicit Wait
		WebDriverWait wait=new WebDriverWait(driver,20);
		
	   WebElement firstPostElement=driver.findElement(By.xpath(postLinkXpath));
		wait.until(ExpectedConditions.visibilityOf(firstPostElement));
		firstPostElement.click();
		
		// Switch back to the first Tab.
		  JavascriptExecutor jse=(JavascriptExecutor)driver; 
		  Set<String> windowsList=driver.getWindowHandles(); 
		  
		  for (String object : windowsList) 
		  {
			  driver.switchTo().window(object);
			  break;
		  }
		  
		  String askQuestionXpath="//*[@id=\"post-156\"]/div/p[5]/a/strong";
		  driver.findElement(By.xpath(askQuestionXpath)).click();
		 
		  String headingXpath="//*[@id=\"ig_close_1621\"]";
		 driver.findElement(By.xpath(headingXpath)).click();		
	}
}

The driver We have used below two statements to achieve this. In the first statement, we have to initiate and define the instance for the WaitDriverWait class.

// Implement Explicit Wait
WebDriverWait wait=new WebDriverWait(<WebDriver>,<TimeDuration>);

  • WebDriver: Reference of WebDriver object.
  • TimeDuration: Integer value of a predefined duration.

Second statement is to define the condition on a particular WebElement instance to wait till it gets load on the screen.

// Check Condition for a predefined time duration wait.until(ExpectedConditions.visibilityOf(<WebElement>));
<WebElement>.click();

  • WebElement: Instance of WebElement for which webdriver is looking for.
  • ExpectedConditions: This contains various type of condition which can be applied as per the requirement. Few examples, given below:
    • elementToBeClickable()
    • visibilityOfElementLocated()
    • titleContains()
    • elementToBeClickable()
    • elementToBeSelected()
Selenium Tutorials:

Window Handling: 

Test Frameworks: 

Related Links: 


803total visits,3visits today

Leave a Reply

Your email address will not be published. Required fields are marked *