Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What does fluent wait perform?
Fluent wait is a dynamic wait mechanism in Selenium that allows the WebDriver to pause and repeatedly check for a condition at regular intervals before throwing an exception. Unlike implicit wait, FluentWait provides more control over polling frequency and exception handling.
How FluentWait Works
FluentWait monitors the DOM at regular intervals (defined by polling frequency) rather than constantly checking. For example, if you set a 30-second timeout with 3-second polling, the driver will check the condition every 3 seconds for up to 30 seconds before timing out.
Syntax
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(3))
.ignoring(NoSuchElementException.class);
Key Components
- withTimeout(): Maximum time to wait before throwing TimeoutException
- pollingEvery(): Frequency at which condition is checked
- ignoring(): Exceptions to ignore during polling
Example
import org.openqa.selenium.By;
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 org.openqa.selenium.NoSuchElementException;
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://www.tutorialspoint.com/index.htm");
// Click on Coding Ground link
driver.findElement(By.xpath("//span[text()='Coding Ground']")).click();
// FluentWait declaration
Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
.withTimeout(Duration.ofSeconds(30))
.pollingEvery(Duration.ofSeconds(3))
.ignoring(NoSuchElementException.class);
// Wait for element with custom condition
WebElement element = wait.until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
WebElement elem = driver.findElement(By.xpath("//img[@title='Whiteboard']"));
if (elem.isDisplayed()) {
return elem;
}
return null;
}
});
System.out.println("Element found and displayed");
} finally {
driver.quit();
}
}
}
Advantages of FluentWait
| Feature | FluentWait | Implicit Wait |
|---|---|---|
| Polling Frequency | Customizable | Fixed (500ms) |
| Exception Handling | Can ignore specific exceptions | Limited |
| Custom Conditions | Yes | No |
| Flexibility | High | Low |
Best Practices
- Use FluentWait when you need custom polling intervals
- Ignore specific exceptions that are expected during polling
- Set reasonable timeout and polling values based on your application
- Use lambda expressions for cleaner custom conditions
Conclusion
FluentWait provides superior control over wait conditions compared to implicit waits. It's ideal for scenarios requiring custom polling frequencies and exception handling in Selenium automation.
