Selenium Waits Tutorial — Everything You Need to Know About Waits in Selenium
By: Neha Vaidya
Tags: Selenium, Testing, Test Automation, Selenium Web Driver, Waits In Selenium

Waits in Selenium — Edureka

While writing your first selenium program, you might have come across wait commands. But, do you know what exactly Selenium waits is? Well, waits in selenium is an essential piece of code that is required to execute a test case. In this article, I will give you a brief insight into different types of wait commands that are widely used in practice.

Below are the topics to be covered in this article:

What is Selenium Waits?

Why Do We Need Waits In Selenium?

Types of Waits

Implicit Waits Explicit Waits

Implicit vs Explicit Waits

What is Selenium Waits?

Waits help the user to troubleshoot issues while re-directing to different web pages. This is achieved by refreshing the entire web page and re-loading the new web elements. At times, there can be Ajax calls as well. Thus, a time lag can be seen while reloading the web pages and reflecting the web elements.

Users are often found navigating through various web pages back and forth. Thus, navigate() commands/methods provided by the WebDriver helps the user to simulate the real time scenarios by navigating between the web pages with reference to the web browser’s history.

Why Do You Need Waits In Selenium?

Most of the web applications are developed using Ajax and JavaScript. When a page is loaded by the browser the elements which we want to interact with may load at different time intervals. With this, it not only becomes difficult to identify the element but also if the element is not located, it will throw an “ElementNotVisibleException” exception. By using Waits, we can resolve this problem.

Now let’s move further and understand different types of waits.

Types of Waits

Selenium supports two types of waits and they are as follows

Implicit Wait Explicit Wait

Note: The most widely used waits are Implicit and Explicit waits and Fluent waits are not preferable for real-time projects.

First, let’s understand what are Implicit waits in Selenium.

Implicit Waits

The Implicit wait will tell the web driver to wait for a certain amount of time before it throws a “No Such Element Exception”. The default setting of Implicit wait is zero. Once you set the time, the web driver will wait for that particular amount of time before throwing an exception.

Syntax: driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);

Let’s take an example of Implicit waits and understand how it works.



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;



public class ImplicitWait{

public static void main(String[] args) throws InterruptedException{

System.setProperty("webdriver.chrome.driver", "C:\\Selenium-java-edureka\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().deleteAllCookies();

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); // pageload timeout

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Implicit Wait for 20 seconds

driver.get("

driver.findElement(By.xpath("//input[

Thread.sleep(1000);

driver.findElement(By.xpath("//input[

}

} package Edureka;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class ImplicitWait{public static void main(String[] args) throws InterruptedException{System.setProperty("webdriver.chrome.driver", "C:\\Selenium-java-edureka\\chromedriver_win32\\chromedriver.exe");WebDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().deleteAllCookies();driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS); // pageload timeoutdriver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); // Implicit Wait for 20 secondsdriver.get(" https://login.yahoo.com/ ");driver.findElement(By.xpath("//input[ @id ='login-username']")).sendKeys(" edureka@yahoo.com "); //Finding element and sending valuesThread.sleep(1000);driver.findElement(By.xpath("//input[ @id ='login-signin']")).click(); //Clicking on the next button if element is located

In the above code, I have given Implicit Wait as 20 seconds, which implies the maximum wait time is 20 seconds for the particular element to load or to arrive at the output.

Note: Implicitly wait is applied globally which means it is always available for all the web elements throughout the driver instance. It implies if the driver is interacting with 100 elements then, Implicitly wait is applicable for all the 100 elements.

This was all about Implicit Waits. Now dive deeper into waits and understand what are explicit waits.

Explicit Waits

It is a concept of the dynamic wait which waits dynamically for specific conditions. It can be implemented by WebDriverWait class. To understand the Explicit wait in Selenium Webdriver, you should know the requirement why we use wait statement in programs. I will give you a couple of examples in which you will get the complete idea of why waits in selenium are important.

Conditions for Explicit wait in selenium web driver

Condition 1 — Suppose I have a web page which has some login form and after login, it takes a lot of time to load Account page or Home page. This page is dynamic it means sometimes it takes 10 seconds to load the homepage, sometimes it’s 15 second and so on. In such situations, Explicit wait helps us to wait until a specific page is not present.

Condition 2 — You are working on travel application and you have filled a web form and clicked on the submit button. Now you have to wait until the specific data is not displayed. In this case, again you can use Explicit wait in which you can give waits till specific or set of elements are not displayed.

Syntax: WebDriverWait wait=new WebDriverWait(WebDriveReference,TimeOut);

In the above syntax, I have created an object of WebDriver wait and passed driver reference and timeout as parameters. This is how you need to write Explicit Waits. Now let’s take an example and understand how explicit wait works. Let’s take a look at the code below.



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.support.ui.ExpectedConditions;

import org.openqa.selenium.support.ui.WebDriverWait;



public class Locators {

public static void main(String[] args) throws InterruptedException {

System.setProperty("webdriver.chrome.driver", "C:\\Selenium-java-edureka\\chromedriver_win32\\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

driver.manage().deleteAllCookies();

driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

driver.get("

WebElement firstname= driver.findElement(By.name("firstname"));

WebElement lastname= driver.findElement(By.name("lastname"));

sendKeys(driver, firstname, 10, "Edureka");

sendKeys(driver, lastname, 20, "Edureka");

WebElement forgotAccount= driver.findElement(By.linkText("Forgotten account?"));

clickOn(driver,forgotAccount, 10);

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}

public static void sendKeys(WebDriver driver1, WebElement element, int timeout, String value){

new WebDriverWait(driver1, timeout).until(ExpectedConditions.visibilityOf(element));

element.sendKeys(value);

}

public static void clickOn(WebDriver driver1, WebElement element, int timeout){

new WebDriverWait(driver1, timeout).until(ExpectedConditions.elementToBeClickable(element));

element.click();

}

} package Edureka;import java.util.concurrent.TimeUnit;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.chrome.ChromeDriver;import org.openqa.selenium.support.ui.ExpectedConditions;import org.openqa.selenium.support.ui.WebDriverWait;public class Locators {public static void main(String[] args) throws InterruptedException {System.setProperty("webdriver.chrome.driver", "C:\\Selenium-java-edureka\\chromedriver_win32\\chromedriver.exe");WebDriver driver = new ChromeDriver();driver.manage().window().maximize();driver.manage().deleteAllCookies();driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);driver.get(" https://www.facebook.com/ ");WebElement firstname= driver.findElement(By.name("firstname"));WebElement lastname= driver.findElement(By.name("lastname"));sendKeys(driver, firstname, 10, "Edureka");sendKeys(driver, lastname, 20, "Edureka");WebElement forgotAccount= driver.findElement(By.linkText("Forgotten account?"));clickOn(driver,forgotAccount, 10);driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);public static void sendKeys(WebDriver driver1, WebElement element, int timeout, String value){new WebDriverWait(driver1, timeout).until(ExpectedConditions.visibilityOf(element));element.sendKeys(value);public static void clickOn(WebDriver driver1, WebElement element, int timeout){new WebDriverWait(driver1, timeout).until(ExpectedConditions.elementToBeClickable(element));element.click();

In the above example, I have used facebook sign up credentials and located them using name locators. Further, I have created a utility or one generic function which will be available for all elements to provide Explicit wait. In the above example, I have written my own sendKeys() method. This method will enter the value in a particular text field, but internally it will provide explicitly wait also. Inside the sendKeys() method, I have given Expected Conditions for visibility of Element. i.e. I am asking the driver to wait for 20 seconds until expected condition visibility of the element. Further, if the condition is satisfied then you can apply sendKeys() to the method. Now, say I want to enter my first and last name. What I will do is, I will use this sendKeys() method and pass the driver, firstname, timeout i.e. 10 seconds and value as edureka. Same goes with lastname as well.

When you execute the program, the chrome driver will launch Google Chrome and navigate through facebook.com and enter the values mentioned in the code. It’s not mandatory to set explicitly wait for a timeout of a particular value, based on your requirement you can change it. This is the major advantage of using explicit wait, but for implicitly wait, once you have defined 10 seconds it will be applicable to all the elements on the webpage and cannot be modified. Same goes with clickOn() method as well. But, this method is useful only for links on the webpage. This is how you can use Explicit Waits.

Note: Implicit, Explicit and Fluent waits, are dynamic waits. What are dynamic waits? Consider a situation where you have given TimeOut value as 20 seconds. If the element is loaded in 5 seconds, then rest 15 seconds will be ignored. It won’t wait till TimeOut value is completed i.e 20 seconds. That’s why all waits are considered as dynamic waits.

Let’s move further and differentiate between Implicit and Explicit Waits.

Implicit vs Explicit Waits

I hope you understood the difference between Implicit and Explicit Waits. This brings us to the end of the article on Waits in Selenium.

If you wish to check out more articles on the market’s most trending technologies like Artificial Intelligence, DevOps, Ethical Hacking, then you can refer to Edureka’s official site.

Do look out for other articles in this series which will explain the various other aspects of Selenium.