πΉ What is Fluent Wait?
Fluent Wait is an advanced version of Explicit Wait that allows custom polling intervals and can ignore specific exceptions while waiting for an element to appear.
Unlike Implicit Wait (which applies globally) and Explicit Wait (which waits for a specific condition), Fluent Wait gives greater control by letting you:
✔ Define timeout duration
✔ Set polling frequency (how often to check the element)
✔ Ignore specific exceptions (like NoSuchElementException
)
π Syntax of Fluent Wait
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(30)) // Maximum wait time
.pollingEvery(Duration.ofSeconds(2)) // Check every 2 seconds
.ignoring(NoSuchElementException.class); // Ignore if element not found
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
πΉ Explanation:
1️⃣ Waits up to 30 seconds for the element
2️⃣ Checks every 2 seconds if the element is available
3️⃣ Ignores NoSuchElementException
, so it doesn’t fail immediately
π When to Use Fluent Wait?
Use Fluent Wait when:
✅ Elements load at unpredictable intervals
✅ You need more flexibility than Implicit/Explicit Wait
✅ You want to avoid waiting unnecessarily if the element appears early
π Fluent Wait with Custom Conditions
You can create custom conditions using Function
instead of ExpectedConditions
.
Example: Waiting Until Element is Clickable
FluentWait<WebDriver> wait = new FluentWait<>(driver)
.withTimeout(Duration.ofSeconds(20))
.pollingEvery(Duration.ofSeconds(2))
.ignoring(NoSuchElementException.class);
WebElement button = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
WebElement btn = driver.findElement(By.id("submitBtn"));
if (btn.isDisplayed() && btn.isEnabled()) {
return btn;
} else {
return null;
}
}
});
button.click();
πΉ Explanation:
✅ Instead of ExpectedConditions
, we use a custom condition inside apply()
✅ The function checks if the button is both visible and enabled
✅ If the condition is met, it returns the WebElement, otherwise, it keeps waiting
π Comparison: Fluent Wait vs Implicit Wait vs Explicit Wait
Feature |
Implicit Wait |
Explicit Wait |
Fluent Wait |
---|---|---|---|
Applied To |
All elements globally |
Specific element |
Specific element |
Condition-Based? |
No |
Yes |
Yes (Customizable) |
Polling Mechanism |
No |
No |
Yes |
Exception Handling |
No |
No |
Yes (Customizable) |
Custom Timeout? |
No |
Yes |
Yes |
π Best Practices for Fluent Wait
✔ Use when elements appear unpredictably
✔ Avoid excessive polling intervals (frequent checks increase CPU usage)
✔ Ignore relevant exceptions (like NoSuchElementException
)
✔ Do not mix Fluent Wait with Implicit Wait (can cause unpredictable behavior)
π Conclusion
Fluent Wait is a powerful synchronization mechanism in Selenium that allows precise control over how WebDriver waits for elements. It is particularly useful for handling dynamic elements and custom polling strategies.
π‘ Use Fluent Wait when:
- Elements load at unpredictable times
- You need custom polling frequency
- You want to ignore specific exceptions
π Master Fluent Wait to make your test automation more stable and efficient! π―