Selenium Wait Commands Using Java : Implicit, Explicit, and Fluent Wait
Last Updated :
30 Jul, 2025
In Selenium WebDriver, wait commands are essential to ensure that elements are present, visible, and interactable before performing actions. Different types of waits help to handle dynamic elements, such as those that load asynchronously.
There are three main wait strategies in Selenium are Implicit Wait, Explicit Wait, and Fluent Wait.
1. Implicit Wait
An Implicit Wait is a global timeout that Selenium will apply to all element searches. This means that Selenium will wait for the specified amount of time before throwing a NoSuchElementException
if an element is not found in the DOM.
Once you set an implicit wait, it remains active for the entire session of the WebDriver instance.
Pros:
- CSS Selector in Selenium is easy to set up with just one line of code and is automatically applied to all element searches. By simply using a CSS selector pattern, you can quickly identify and interact with elements based on their attributes, classes, or structure.
- This makes it a seamless and efficient way to locate elements during automation, reducing the need for multiple lines of code and ensuring smooth, fast execution of tests.
Cons:
- CSS Selector in Selenium is efficient, it can have drawbacks. If the selector is too long or overly complex, it may slow down the tests unnecessarily, especially when working with large DOM structures.
- It does not offer fine-grained control over specific conditions like checking if an element is visible or clickable, which means you may need additional checks or conditions in your code to ensure the element is ready for interaction.
Example: Implicit Wait on saucedemo.com
Java
package Tests;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ImplicitWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Set Implicit Wait for 10 seconds
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Find the username field (Implicit wait will apply here)
WebElement usernameField = driver.findElement(By.id("user-name"));
// Enter username
usernameField.sendKeys("standard_user");
System.out.println("Username entered successfully.");
} finally {
// Close browser
driver.quit();
}
}
}
Output:
Implicit Wait Output2. Explicit Wait
An Explicit Wait allows you to wait for a specific condition to be met before proceeding with the next step in the test. Unlike implicit waits, you can use this wait for individual elements with specific conditions, such as the element being visible or clickable.
Explicit waits are more precise and efficient, as they only apply to the specific conditions you define.
Pros:
- Explicit wait in Selenium offers more precision by waiting only for specific conditions, such as visibility or clickability of elements.
- It is faster than implicit wait because it targets particular elements rather than applying a global wait to all elements
- Explicit wait provides clear error messages, such as TimeoutException, when the specified condition is not met, making it easier to debug and handle test failures effectively.
Cons:
- Explicit wait requires more code compared to implicit waits because you need to specify the conditions for each wait
- This adds complexity, as you must define what exactly you're waiting for (e.g., visibility, clickability, presence) for every element.
Example: Explicit Wait on saucedemo.com
Python
package Tests;
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.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import java.time.Duration;
public class ExplicitWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Wait up to 10 seconds for username field to be clickable
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement usernameField = wait.until(
ExpectedConditions.elementToBeClickable(By.id("user-name"))
);
// Enter username
usernameField.sendKeys("standard_user");
System.out.println("Username entered successfully.");
} finally {
// Close browser
driver.quit();
}
}
}
Output:
Explicit Wait Output3. Fluent Wait
A Fluent Wait is a more flexible version of the explicit wait. It allows you to configure how frequently Selenium checks the condition (polling interval), how long it waits before timing out (maximum wait time), and which exceptions it should ignore (like NoSuchElementException
).
This is ideal for situations where elements load unpredictably or asynchronously.
Pros:
- Fluent waits are highly customizable, offering control over polling intervals and exception handling.
- They stop as soon as the condition is met, improving efficiency, making them ideal for elements that load unpredictably or after varying intervals.
- This flexibility allows for more precise and optimized waiting behavior in Selenium tests.
Cons:
- Fluent waits are more complex to write compared to implicit or explicit waits.
- They require careful tuning of polling intervals to ensure optimal performance, especially when dealing with elements that load at unpredictable times.
Example: Fluent Wait on saucedemo.com
Java
package Tests;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.NoSuchElementException;
import java.time.Duration;
public class FluentWaitTest {
public static void main(String[] args) {
// Set up ChromeDriver
WebDriver driver = new ChromeDriver();
try {
// Navigate to Saucedemo
driver.get("https://www.saucedemo.com/");
// Enter invalid credentials and click login
driver.findElement(By.id("user-name")).sendKeys("invalid_user");
driver.findElement(By.id("password")).sendKeys("wrong_password");
driver.findElement(By.id("login-button")).click();
// Fluent wait for error message
Wait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(10)) // Max wait time
.pollingEvery(Duration.ofSeconds(1)) // Check every 1 second
.ignoring(NoSuchElementException.class); // Ignore this exception
// Use a different name for the parameter in the lambda expression
WebElement errorMessage = wait.until(
wd -> wd.findElement(By.cssSelector("h3[data-test='error']"))
);
System.out.println("Error message: " + errorMessage.getText());
} finally {
// Close browser
driver.quit();
}
}
}
Output:
Fluent Wait outputSelenium WebDriver’s wait commands Implicit, Explicit, and Fluent Waits make testing dynamic web apps in Java reliable and efficient. Implicit waits are simple but broad, explicit waits offer precision, and fluent waits provide flexibility for complex scenarios. By using these waits correctly, you can make your Selenium tests more reliable and efficient, ensuring they work seamlessly with modern, dynamic web applications.
Similar Reads
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING