Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

Implementation of Explicit Wait as FluentWait and WebDriverWait in Selenium WebDriver

Posted on 02/19/2025 By admin

Hello Folks,

In this post we are going to see something interesting concepts about waits in Selenium WebDriver.

Generally Selenium professionals know that there are three types of waits in Selenium WebDriver:-

  1. Implicit Wait
  2. Explicit Wait
  3. Fluent Wait

In fact there are only two types of waits:-

  1. Implicit wait
  2. Explicit wait

We can implement Explicit wait in two ways:-

Selenium official document talks the same. They have not mentioned Fluent wait separately .

Brief about both waits as per Selenium Documentation:-

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("some URL");
WebElement myDynamicElement = driver.findElement(By.id("someID"));

An explicit wait is code you define to wait for a certain condition to occur before proceeding further in the code. WebDriverWait in combination with ExpectedCondition (Note- ExpectedCondition is an interface while
ExpectedConditions is a class) is one way to do this. That condition can be implemented either using ExpectedConditions methods or override apply() methods and write your own condition.

Example Code:-

WebDriver driver = new FirefoxDriver();
driver.get("some url");
WebDriverWait wait = new WebDriverWait(driver, 10);
// Using readymade wat conditions
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("someID")));
// Using own way of waiting
boolean foo = wait.until(new Function < WebDriver, Boolean > () {
 public boolean apply(WebDriver driver) {
  return driver.findElement(By.id("foo")).isDisplayed();
 }
});

FluentWait is a class in Selenium WebDriver which implements Wait interface and WebDriverWait is direct known subclass to it. It means all are in same hierarchy. Fluent wait is an implementation of the Wait interface that may have its timeout and polling interval configured on the fly. WebDriverWait is a specialization of FluentWait that uses WebDriver instances. To be more clear, FluentWait is a generic class (public class FluentWait implements Wait) while WebDriverWait is specialization of FluentWait (public class WebDriverWait extends FluentWait). WebDriverWait is designed to work for WebDriver exclusively.

So if we implement Explicit wait using either FluentWait or WebDriverWait, we can configure with desired polling interval and ignore one or more exceptions. Refer below examples:-

Wait wait = new FluentWait(driver).withTimeout(Duration.ofSeconds(30)) .pollingEvery(Duration.ofSeconds(5))
.ignoring(NoSuchElementException.class);
WebElement foo = wait.until(new Function() { public WebElement apply(WebDriver driver) { return driver.findElement(By.id("foo")); }
});
WebDriver driver = new FirefoxDriver();
driver.get("Some URL");
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.id("someID")));

We can use either readymade methods available in ExpectedConditions class or we can create our own wait conditions by overriding apply() method of Function interface in until() method. Maximum wait conditions methods of ExpectedConditions class overrides apply() method.

FluentWait class has different methods to configure wait instance. For example:- withTimeout(), pollingEvery(), ignoring() etc. This class is generic so all methods as well.

In WebDriverWait class which is specialization of FluentWait with WebDriver and provides a way to configure wait instance easily. For example:-

When we write below line:-

WebDriverWait wait = new WebDriverWait(driver, 30);

It calls constructor of FluentWait and set default polling interval time (DEFAULT_SLEEP_TIMEOUT) which is 500 ms and ignore instances of NotFoundException that are encountered (thrown) by default in the ‘until’ condition. You can change default polling interval and ignoring exceptions as well as shown below:-

WebDriverWait wait1 = new WebDriverWait(driver,30);wait1.pollingEvery(Duration.ofSeconds(2));wait1.ignoring(ElementClickInterceptedException.class);

You can also user overloaded WebDriverWait constructor to set interval time as below:-

WebDriverWait wait1= new WebDriverWait(driver, 30, 5000);

Above statement works in the same way except instead of setting default polling interval time as 500 ms. It will set polling interval time as 5 seconds.

So to summarize, WebDriverWait is a FluentWait (Inheritance) and FluentWait is Explicit wait in Selenium WebDriver.

It is also not correct that in WebDriverWait we can use only ExpectedConditions class wait methods and in FluentWait , we can use only apply() method. We can set custom polling interval and ignore exceptions in both implementation i.e. WebDriverWait & FluentWait.

Explicit Wait as WebDriverWait with ExpectedConditions and apply() method:-

package WaitExamples; import java.time.Duration;
import java.util.Date;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait; import io.github.bonigarcia.wdm.WebDriverManager; public class ExplicitWaitAsWebDriverWaitExample { public static void main(String[] args) throws InterruptedException { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://www.itgeared.com/demo/1502-javascript_countdown.html"); // Configuring a WebDriverWait with polling interval and ignoring exception WebDriverWait wait = new WebDriverWait(driver,30); wait.pollingEvery(Duration.ofMillis(5000)); wait.ignoring(ElementNotInteractableException.class); // Using ready made wait conditions of ExpectedCOnditions class wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("myBtn")))); System.out.println("Element is ready to be clicked."); // Refreshing the page driver.get("http://www.itgeared.com/demo/1502-javascript_countdown.html"); // Overriding apply() method to created our own wait condition for WebDriverWait boolean isClickable = wait.until(new Function() { public Boolean apply(WebDriver driver) { System.out.println("Interval time : "+new Date()); String text= driver.findElement(By.id("myBtn")).getText(); if(text.equals("Click Me!")) return true; return false; } }); if(isClickable) System.out.println("Element is ready to be clicked."); driver.quit(); }
} 

Output:-

Element is ready to be clicked.Interval time : Thu Jun 20 06:51:08 IST 2019Interval time : Thu Jun 20 06:51:13 IST 2019Interval time : Thu Jun 20 06:51:18 IST 2019Interval time : Thu Jun 20 06:51:23 IST 2019
Element is ready to be clicked.

You can see how we set custom polling interval in WebDriverWait.

Explicit Wait as WebDriverWait with ExpectedConditions and apply() method:-

package WaitExamples; import java.time.Duration;
import java.util.Date;
import java.util.NoSuchElementException;
import java.util.function.Function; import org.openqa.selenium.By;
import org.openqa.selenium.ElementNotInteractableException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait; import io.github.bonigarcia.wdm.WebDriverManager; public class ExplicitWaitAsFluentWaitExample { public static void main(String[] args) throws InterruptedException { WebDriverManager.chromedriver().setup(); WebDriver driver = new ChromeDriver(); driver.manage().window().maximize(); driver.get("http://www.itgeared.com/demo/1502-javascript_countdown.html"); // Configuring a FluentWait with polling interval and ignoring exception Wait wait = new FluentWait(driver) .withTimeout(Duration.ofMillis(30000)) .pollingEvery(Duration.ofMillis(5000)) .ignoring(NoSuchElementException.class); // Using ready made wait conditions of ExpectedCOnditions class wait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.id("myBtn")))); System.out.println("Element is ready to be clicked."); // Refreshing the page driver.get("http://www.itgeared.com/demo/1502-javascript_countdown.html"); // Overriding apply() method to created our own wait condition for WebDriverWait boolean isClickable = wait.until(new Function() { public Boolean apply(WebDriver driver) { System.out.println("Interval time : "+new Date()); String text= driver.findElement(By.id("myBtn")).getText(); if(text.equals("Click Me!")) return true; return false; } }); if(isClickable) System.out.println("Element is ready to be clicked."); driver.quit(); }
} 

#ThanksForReading

Uncategorized

Post navigation

Previous Post: Handle Horror Of NullPointerException Using Optional Class
Next Post: Interview Experience at Home Credit India – Gurgaon for Automation Testing Profile – Nov– 2019

Related Posts

#2. OAuth 2.0 Flow – Authorization Grants And Their Types Uncategorized
Make Selenium Easy – Page 7 Uncategorized
image – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
Postman Tutorial Part 44 – Data Driven Testing in Postman Using CSV File – Make Selenium Easy Uncategorized
xpathInDOM – Make Selenium Easy Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark