</>

Technology

Selenium

Difficulty

Intermediate

Interview Question

How to implement Fluent Wait in Selenium?

Fluent Wait provides configurable polling intervals and exception ignoring for waiting on dynamic elements.

Answer

Fluent Wait is an advanced form of Explicit Wait with a customizable polling interval and the ability to ignore specific exceptions during polling.

Full implementation:

Java
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import java.time.Duration;

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

WebElement element = fluentWait.until(
    driver -> driver.findElement(By.id("dynamicResult"))
);

Custom function wait:

Java
fluentWait.until(new Function<WebDriver, Boolean>() {
    public Boolean apply(WebDriver driver) {
        String count = driver.findElement(By.id("counter")).getText();
        return count.equals("100");
    }
});

Fluent vs Explicit:

Explicit WaitFluent Wait
Polling intervalFixed (~500ms)Customizable
Ignore exceptionsNoYes
Use caseMost scenariosAJAX, heavy dynamic pages

Follow AutomateQA

Related Topics