"Automation is not about replacing jobs; it's about empowering people to do more meaningful work." — Unknown

Explicit Wait

Estimated reading: 5 minutes 15 views

Overview

In Selenium WebDriver, Explicit Wait is a type of wait that allows the WebDriver to wait for a specific condition to be met before continuing with the execution of the script. Unlike Implicit Wait, which applies globally to all elements throughout the session, Explicit Wait is used for specific elements based on certain conditions. This wait is particularly useful for situations where elements may take a varying amount of time to load, and you want to wait for a specific condition, like visibility, clickability, or the presence of an element.

Syntax

Explicit wait is implemented using the WebDriverWait class along with ExpectedConditions. The general syntax is:

				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.elementToBeClickable(By.id("exampleId")));
				
			

In the example above:

  • WebDriverWait defines how long to wait (in this case, 10 seconds).
  • ExpectedConditions.elementToBeClickable is the condition to wait for (this could be other conditions like visibility, presence, etc.).

Usage

  1. Waiting for an Element to Be Clickable:

    If you want to wait for a specific element to be clickable before performing an action, you can use the elementToBeClickable() condition.

				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitButton")));
button.click();
				
			

In this case, the WebDriver will wait up to 10 seconds for the button with the id="submitButton" to become clickable. If the button becomes clickable within the time frame, it will be clicked.

2. Waiting for Element Visibility:

Another common scenario is waiting for an element to be visible before interacting with it.

				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("exampleId")));
				
			

This will make Selenium wait until the element with the ID exampleId becomes visible before interacting with it.

3. Waiting for Element to Be Present in the DOM:

If you only want to check for the presence of an element in the DOM (even if it is not visible), you can use presenceOfElementLocated()

				
					WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("exampleId")));
				
			

Example

Here’s an example of using Explicit Wait to wait for a button to be clickable before clicking it:

				
					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.WebDriverWait;

import java.time.Duration;

public class ExplicitWaitExample {
    public static void main(String[] args) {
        // Set path to the ChromeDriver executable
        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        // Initialize WebDriver
        WebDriver driver = new ChromeDriver();

        // Open the website
        driver.get("https://www.example.com");

        // Create an instance of WebDriverWait
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Wait until the button is clickable
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitButton")));

        // Click the button once it is clickable
        button.click();

        // Close the browser
        driver.quit();
    }
}

				
			

Importance

  1. Handles Dynamic Content: Modern web applications often load elements dynamically (such as content loaded via JavaScript or AJAX). Explicit wait allows you to wait for these elements to meet a specific condition (such as visibility or clickability) before interacting with them.

  2. Prevents Exceptions: Explicit wait helps to prevent exceptions like ElementNotVisibleException, ElementNotInteractableException, and TimeoutException by waiting for elements to meet specific conditions, reducing flakiness in automated tests.

  3. More Control Over Waiting: With explicit wait, you have more control over the waiting process. You can specify the exact condition you are waiting for (e.g., element to be visible, clickable, etc.) and apply it to a specific element, unlike implicit wait, which applies globally.

Limitations

  1. Performance Impact: Explicit wait involves more specific conditions that can cause delays in execution if not used properly. For example, if you wait for an element that never becomes visible or clickable, your test might hang for the entire wait time.

  2. Complexity: While it offers more control, explicit wait can also increase the complexity of the code, especially when used for multiple conditions or elements in the test script.

  3. Requires Frequent Updates: As web pages and web elements evolve, it may be necessary to frequently update the waiting conditions (e.g., if the element ID changes or new elements are added).

Differences Between Implicit and Explicit Wait

  • Implicit Wait: Applied globally to all elements for a specified duration. It waits for elements to appear or become interactable before continuing the script. It cannot be fine-tuned for specific conditions.

  • Explicit Wait: Used for specific elements and can wait for various conditions, such as visibility, clickability, or presence of an element. It offers more flexibility and precision compared to implicit wait.

Conclusion

The Explicit Wait method in Selenium WebDriver is a powerful tool for handling dynamic web elements and ensuring that elements are in the expected state before interacting with them. It is particularly useful for web pages where elements take varying amounts of time to load, such as those using AJAX or JavaScript to update content.

By using explicit wait, testers can improve the stability and reliability of their automation scripts, making them more resilient to timing issues. It is an essential feature for any Selenium WebDriver test suite, especially when working with complex web applications.

Key Points:

  • Explicit Wait waits for a specific condition to be true before proceeding with the next step.
  • It allows for fine-tuned waiting based on conditions like visibility, presence, or clickability of elements.
  • Explicit wait is more flexible and precise compared to implicit wait, which applies globally.

Leave a Comment

Share this Doc

Explicit Wait

Or copy link

CONTENTS