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 Wait | Fluent Wait | |
|---|---|---|
| Polling interval | Fixed (~500ms) | Customizable |
| Ignore exceptions | No | Yes |
| Use case | Most scenarios | AJAX, heavy dynamic pages |
