Introduction to Selenium
Selenium WebDriver Basics
WebDriver Commands
Synchronization in Selenium
Working with Different Browsers
Setting up WebDriver for different browsers
Handling Advanced User Interactions
Page Object Model (POM)
Introduction to POM
TestNG Framework
Creating and Running TestNG Tests

Fluent wait in Selenium is a type of wait that allows you to define the maximum amount of time to wait for a condition to be met, as well as the frequency with which to check the condition before throwing an exception if the condition is not met. This type of wait is useful when the condition to be met might take varying amounts of time to be fulfilled

Key Features of Fluent Wait

  • Polling Interval: The frequency with which the condition is checked.
  • Timeout: The maximum amount of time to wait for the condition.
  • Ignoring Exceptions: The ability to ignore specific types of exceptions while waiting for the condition.

Implementation

In Java, you use the ‘FluentWait‘ class to implement a fluent wait. Here’s a detailed 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.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import java.time.Duration;
import java.util.NoSuchElementException;
import java.util.function.Function;

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

        try {
            // Navigate to the website
            driver.get("https://www.example.com");

            // Define Fluent Wait
            FluentWait<WebDriver> wait = new FluentWait<>(driver)
                    .withTimeout(Duration.ofSeconds(30)) // Max wait time
                    .pollingEvery(Duration.ofSeconds(5)) // Check interval
                    .ignoring(NoSuchElementException.class); // Ignore specific exceptions

            // Define the condition to wait for
            WebElement element = wait.until(new Function<WebDriver, WebElement>() {
                public WebElement apply(WebDriver driver) {
                    return driver.findElement(By.id("someElementId"));
                }
            });

            // Perform actions on the element
            element.click();

        } finally {
            // Close the driver
            driver.quit();
        }
    }
}

				
			

Explanation

  1. Setup the Chrome driver: This step involves setting the path to the ChromeDriver executable.
  2. Navigate to the website: The get method is used to open the specified URL.
  3. Define Fluent Wait:
    • withTimeout(Duration.ofSeconds(30))‘: The maximum time to wait for the condition.
    • pollingEvery(Duration.ofSeconds(5))‘: The interval between each check.
    • ignoring(NoSuchElementException.class)‘: Ignore ‘NoSuchElementException‘ while waiting.
  4. Define the condition: The ‘until‘ method takes a function that returns the WebElement once it is found. This function is called repeatedly at the specified interval until the element is found or the timeout is reached.
  5. Perform actions on the element: Once the element is found, you can perform actions on it, such as clicking.
  6. Close the driver: The ‘quit‘ method is used to close the browser.

Advantages of Fluent Wait

  • Customizable Polling Interval: You can set how frequently Selenium checks for the condition.
  • Exception Handling: You can specify which exceptions to ignore during the wait.
  • Flexibility: Fluent Wait provides more control over the waiting process compared to other types of waits like implicit or explicit waits.

Fluent wait is especially useful in scenarios where elements may appear at unpredictable times or when dealing with dynamic content that may take varying amounts of time to load.

Scroll to Top