0
0
Selenium Javatesting~5 mins

FluentWait with polling in Selenium Java

Choose your learning style9 modes available
Introduction

FluentWait helps you wait for something on a web page by checking regularly until it appears or a timeout happens. It lets you control how often to check.

When a web element takes time to appear and you want to check repeatedly until it shows up.
When you want to ignore certain errors while waiting, like element not found.
When you want to set a custom time interval between checks instead of default.
When you want to wait for a condition that is not just presence but something custom.
When you want to avoid waiting too long by setting a maximum timeout.
Syntax
Selenium Java
Wait<WebDriver> wait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

WebElement element = wait.until(driver -> driver.findElement(By.id("elementId")));

withTimeout sets the maximum wait time.

pollingEvery sets how often to check the condition.

Examples
Waits up to 20 seconds, checking every 2 seconds for an element with class 'my-class'.
Selenium Java
Wait<WebDriver> wait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(20))
    .pollingEvery(Duration.ofSeconds(2))
    .ignoring(NoSuchElementException.class);

WebElement element = wait.until(driver -> driver.findElement(By.cssSelector(".my-class")));
Waits up to 10 seconds, checking every 0.5 seconds if the button with id 'btnSubmit' is visible.
Selenium Java
Wait<WebDriver> wait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(10))
    .pollingEvery(Duration.ofMillis(500))
    .ignoring(NoSuchElementException.class);

Boolean isVisible = wait.until(driver -> driver.findElement(By.id("btnSubmit")).isDisplayed());
Sample Program

This program opens a browser, goes to example.com, and waits up to 15 seconds checking every 3 seconds for an element with id 'dynamicElement'. If found, it prints the element's text. If not found in time, it prints a message.

Selenium Java
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 java.time.Duration;
import java.util.function.Function;

public class FluentWaitExample {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");
        WebDriver driver = new ChromeDriver();

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

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

            WebElement dynamicElement = wait.until(new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                    return driver.findElement(By.id("dynamicElement"));
                }
            });

            System.out.println("Element found: " + dynamicElement.getText());
        } catch (Exception e) {
            System.out.println("Element not found within timeout.");
        } finally {
            driver.quit();
        }
    }
}
OutputSuccess
Important Notes

Always set a reasonable timeout to avoid long waits.

Polling interval should balance between responsiveness and resource use.

Ignoring exceptions helps avoid test failure during waiting.

Summary

FluentWait lets you wait for elements with custom timeout and polling.

You can ignore exceptions while waiting to keep tests stable.

Use FluentWait when elements load dynamically and timing is uncertain.