Open In App

Selenium Wait Commands Using Java : Implicit, Explicit, and Fluent Wait

Last Updated : 30 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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-output
Implicit Wait Output

2. 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-in-selenium
Explicit Wait Output

3. 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-output
Fluent Wait output

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


Article Tags :

Similar Reads