Chapter 3: Handling Waits and Synchronization
Under the hood, WebDriverWait is actually a specialized subclass of FluentWait. FluentWait provides maximum control over the polling engine: configuring the polling frequency (e.g. check every 100ms instead of 500ms), specifying which exceptions to ignore during the wait cycle, and setting descriptive timeout error messages.
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
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(20)) // Maximum duration
.pollingEvery(Duration.ofMillis(200)) // Poll frequency (default is 500ms)
.ignoring(NoSuchElementException.class) // Suppress during wait cycle
.ignoring(StaleElementReferenceException.class) // Suppress DOM re-render glitches
.withMessage("Timed out waiting for NetBanking transfer confirmation modal to render");
WebElement confirmationModal = fluentWait.until(d -> {
WebElement modal = d.findElement(By.id("transfer-confirmation-modal"));
return modal.isDisplayed() ? modal : null;
});Key Point: FluentWait gives you full control over polling frequency, ignored exceptions, and custom failure diagnostics. Use it for complex dynamic SPAs.