Testing With Selenium WebDriver (82 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (72 Blogs)
SEE MORE

Everything You Need to Know About Waits in Selenium

Last updated on Sep 11,2024 31.7K Views

A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop. A tech enthusiast in Java, Image Processing, Cloud Computing, Hadoop.
6 / 8 Blog from Locators and Waits in Selenium

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.

Waits in Selenium | Selenium Wait Commands

This video talks about different types of selenium waits and steps involved to use implicit and explicit waits along with examples.

What is Selenium Waits? 

Selenium logo - EdurekaWaits 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.

Find out our Selenium Training in Top Cities/Countries

IndiaOther Cities/Countries
BangaloreUS
HyderabadUK
PuneCanada
ChennaiAustralia
MumbaiSingapore
KolkataEdinburgh

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

  1. Implicit Wait
  2. Explicit Wait

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

Types of Waits in Selenium-Waits in Selenium

 

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. 

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-edurekachromedriver_win32chromedriver.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("https://login.yahoo.com/");
driver.findElement(By.xpath("//input[@id='login-username']")).sendKeys("edureka@yahoo.com"); //Finding element and sending values
Thread.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.

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-edurekachromedriver_win32chromedriver.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

Implicit WaitsExplicit Waits
1. Implicit Wait time is applied to all the elements in the script1. Explicit Wait time is applied only to those elements which are specified by the user
2. In Implicit Wait, we need not specify “ExpectedConditions” on the element to be located2. In Explicit Wait, we need to specify “ExpectedConditions” on the element to be located
3. It is recommended to use when the elements are located with the time frame specified in implicit wait3. It is recommended to use when the elements are taking a long time to load and also for verifying the property of the element like (visibilityOfElementLocated, elementToBeClickable,elementToBeSelected)

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

If you wish to learn Selenium and build a career in the testing domain, then check out our interactive, live-online Selenium Certification Training here, that comes with 24*7 support to guide you throughout your learning period.

Got a question for us? Please mention it in the comments section of Waits in Selenium blog and we will get back to you.

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 23rd November,2024

23rd November

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 21st December,2024

21st December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need to Know About Waits in Selenium

edureka.co