Saturday, December 5, 2026

Fluent Wait in Selenium Java

Fluent Wait in Selenium Java

1. What is Fluent Wait in Selenium?

Fluent Wait in Selenium Java is an explicit wait mechanism that allows us to define the maximum amount of time to wait for a particular condition and the frequency at which Selenium checks that condition.

It also allows us to ignore specific exceptions while waiting for an element.

Fluent Wait is useful when web elements take an unpredictable amount of time to become available, especially in dynamic web applications.

For example:

  • Waiting for an element to become visible.
  • Waiting for an element to become clickable.
  • Waiting for an element to disappear.
  • Waiting for dynamically loaded elements.
  • Ignoring NoSuchElementException while polling.

Fluent Wait in Selenium Java


2. Syntax of Fluent Wait

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(5))
        .ignoring(NoSuchElementException.class);

Explanation:

Method Description
withTimeout() Defines the maximum waiting time.
pollingEvery() Defines how frequently Selenium checks the condition.
ignoring() Specifies exceptions to ignore while waiting.
until() Waits until the specified condition is satisfied.

3. Fluent Wait Example in Selenium Java

Let's consider an example where Selenium waits for an element to become visible before performing an action.

import java.time.Duration;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class FluentWaitExample {

    public static void main(String[] args) {

        WebDriver driver = new ChromeDriver();

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

        driver.get("https://example.com");

        // Create Fluent Wait
        Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
                .withTimeout(Duration.ofSeconds(30))
                .pollingEvery(Duration.ofSeconds(5))
                .ignoring(NoSuchElementException.class);

        // Wait until element becomes visible
        WebElement element = wait.until(
                ExpectedConditions.visibilityOfElementLocated(
                        By.id("username")
                )
        );

        // Perform action
        element.sendKeys("admin");

        driver.quit();
    }
}

How does this code work?

  1. Selenium launches the Chrome browser.
  2. Opens the application URL.
  3. Creates a Fluent Wait object.
  4. Sets the maximum waiting time to 30 seconds.
  5. Checks the condition every 5 seconds.
  6. Ignores NoSuchElementException.
  7. Waits until the username field becomes visible.
  8. Enters the username.

4. Fluent Wait with Different Expected Conditions

You can use Fluent Wait with Selenium's ExpectedConditions class.

Example 1: Wait for element visibility

WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(
        By.id("username")
    )
);

Example 2: Wait for element to be clickable

WebElement element = wait.until(
    ExpectedConditions.elementToBeClickable(
        By.id("login")
    )
);

element.click();

Example 3: Wait for element presence

WebElement element = wait.until(
    ExpectedConditions.presenceOfElementLocated(
        By.id("username")
    )
);

Example 4: Wait for element invisibility

wait.until(
    ExpectedConditions.invisibilityOfElementLocated(
        By.id("loader")
    )
);

Example 5: Wait for title to contain text

wait.until(
    ExpectedConditions.titleContains("Dashboard")
);

5. Fluent Wait Without ExpectedConditions

We can also implement Fluent Wait using a custom condition with a lambda expression.

Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(2))
        .ignoring(NoSuchElementException.class);

WebElement element = wait.until(d -> {

    WebElement username = d.findElement(By.id("username"));

    if (username.isDisplayed()) {
        return username;
    }

    return null;
});

element.sendKeys("admin");

Here:

  • d represents the WebDriver instance.
  • findElement() locates the element.
  • isDisplayed() checks whether the element is visible.
  • Returning the element means the condition is satisfied.
  • Returning null means Selenium continues polling.

6. Fluent Wait with Multiple Ignored Exceptions

Fluent Wait allows us to ignore multiple exceptions.

Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(3))
        .ignoring(NoSuchElementException.class)
        .ignoring(StaleElementReferenceException.class);

Import statements:

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;

This is useful when elements are dynamically created or refreshed.

7. Difference Between Implicit Wait, Explicit Wait and Fluent Wait

Feature Implicit Wait Explicit Wait Fluent Wait
Waiting type Global Condition-based Condition-based
Scope All element lookups Specific condition Specific condition
Polling interval Selenium controlled Default polling Customizable
Ignore exceptions No custom configuration Limited through conditions Configurable
Custom conditions No Yes Yes
Maximum timeout Configurable Configurable Configurable
Best use Basic element lookup Most synchronization scenarios Dynamic elements and custom polling

Important: Fluent Wait is a configurable form of explicit wait. In Selenium 4, WebDriverWait itself extends FluentWait.

8. Real-Time Interview Scenario

Question: Suppose an element takes 2 seconds sometimes, 5 seconds sometimes, and 15 seconds sometimes to load. How would you handle this scenario?

Answer:

I would use Fluent Wait because it allows me to configure the maximum timeout and polling interval.

Wait<WebDriver> wait = new FluentWait<>(driver)
        .withTimeout(Duration.ofSeconds(30))
        .pollingEvery(Duration.ofSeconds(2))
        .ignoring(NoSuchElementException.class);

WebElement element = wait.until(
        ExpectedConditions.visibilityOfElementLocated(
                By.id("dynamicElement")
        )
);

element.click();

In this example:

  • Maximum timeout: 30 seconds.
  • Polling interval: 2 seconds.
  • Selenium checks the element repeatedly.
  • Once the element becomes visible, Selenium proceeds immediately.
  • If the condition is not satisfied within the timeout, Selenium throws a TimeoutException.

9. Important Interview Questions

Q1. What is Fluent Wait in Selenium?

Fluent Wait is a synchronization mechanism that waits for a condition with a configurable timeout, polling interval, and ignored exceptions.

Q2. Can we change the polling interval in Fluent Wait?

Yes. We can use pollingEvery() to configure the polling interval.

Q3. Can Fluent Wait ignore multiple exceptions?

Yes. We can chain multiple ignoring() methods.

Q4. What happens if the element is not found within the maximum timeout?

Selenium throws a TimeoutException.

Q5. What is the default polling interval of Fluent Wait?

The default polling interval is 500 milliseconds in Selenium's FluentWait implementation.

Q6. Is WebDriverWait a child class of FluentWait?

Yes. In Selenium 4, WebDriverWait extends FluentWait<WebDriver>.

Q7. Can we use Fluent Wait without ExpectedConditions?

Yes. We can use custom conditions with lambda expressions.

10. Best Practices

  1. Prefer explicit waits over Thread.sleep().
  2. Use meaningful timeout values based on application performance.
  3. Configure polling intervals according to the scenario.
  4. Ignore only exceptions that are expected during polling.
  5. Avoid mixing implicit and explicit waits because it can cause unpredictable wait durations.
  6. Use custom conditions for complex synchronization requirements.

Interview tip: Remember this simple formula:

Fluent Wait = Maximum Timeout + Polling Interval + Ignored Exceptions + Condition

  • Maximum timeout: 30 seconds
  • Polling interval: 2 seconds
  • Ignored exception: NoSuchElementException
  • Condition: Element visibility

No comments:

Post a Comment