Waiting Techniques in Selenium WebDriver with Java

| | 2 min read

In Selenium WebDriver using Java, multiple methods exist to wait for elements to become present, visible, clickable, or until specific conditions are fulfilled. Using waits is necessary to handle elements to appear before taking actions with automation. You should think about using waits when if you are getting "Element Not Visible Exception"

Types of waits and examples using Java

 

 

Implicit Wait

Implicit waits are globally set for the WebDriver instance and are applied to all elements. They instruct WebDriver to wait for a certain amount of time before throwing a "No Such Element" exception.

WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://example.com");
WebElement element = driver.findElement(By.id("elementId"));

Explicit Wait with ExpectedConditions

Explicit waits allow WebDriver to wait for a certain condition to occur before proceeding further in the code. ExpectedConditions provides a variety of conditions to wait for, such as element visibility, presence, clickability, etc.

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
// Perform actions on the element

Fluent Wait

Fluent waits are more flexible than explicit waits because they allow the system to wait at regular intervals until the condition is met. With fluent waits, you can specify both the maximum wait time and the frequency of polls.

WebDriver driver = new ChromeDriver();
driver.get("https://example.com");

Wait<WebDriver> fluentWait = new FluentWait<>(driver)
    .withTimeout(Duration.ofSeconds(30))
    .pollingEvery(Duration.ofSeconds(5))
    .ignoring(NoSuchElementException.class);

WebElement fluentElement = fluentWait.until(new Function<WebDriver, WebElement>() {
    public WebElement apply(WebDriver driver) {
        return driver.findElement(By.id("elementId"));
    }
});
// Perform actions on the fluentElement

Choosing the Right Wait

Each type of wait has its advantages and can be utilized based on the specific requirements of the test case. Implicit waits are simple and easy to use but can potentially slow down test execution. Explicit waits are more versatile and allow for more complex conditions, while Fluent waits offer the most flexibility in terms of polling frequency and ignoring exceptions.

It's essential to adjust timeouts and conditions according to the application's behavior in order to create stable and reliable automation scripts. Note that mixing implicit and explicit waits can lead to unpredictable wait times and is generally discouraged. As a best practice, use explicit waits to handle dynamic content where the wait time may vary, ensuring that your tests remain as efficient as possible.

If you are looking for professional support in automating your test cases, do contact us.