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

In Selenium WebDriver for Java, an explicit wait is a type of synchronization that allows you to pause your test execution until a specific condition is met or a maximum amount of time has elapsed. Unlike implicit waits that set a default wait time for the entire duration of the WebDriver session, explicit waits are used to wait for a particular condition to occur before proceeding with the next step.

Explicit waits are useful when you need to wait for a certain element to become visible, clickable, or present on the page. The ‘WebDriverWait‘ class is used in combination with the ‘ExpectedConditions‘ class to achieve this.

Key Components of Explicit Wait

  • WebDriverWait: This is the main class that implements the explicit wait functionality. It allows you to specify the maximum wait time and the polling interval.
  • ExpectedConditions: This class provides a set of predefined conditions that can be used with ‘WebDriverWait‘. Some common conditions include:
    • visibilityOfElementLocated
    • elementToBeClickable
    • presenceOfElementLocated'
    • titleContains

Example Usage

Here’s an example of how to use explicit waits in Selenium WebDriver with Java:

Step-by-Step Example

1. Import Required Packages

				
					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;

				
			

2. Set Up WebDriver

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

        // Create a new instance of the Chrome driver
        WebDriver driver = new ChromeDriver();

        // Open a website
        driver.get("http://www.example.com");
        
        // Maximize the browser window
        driver.manage().window().maximize();
        
        // Call the method to demonstrate explicit wait
        demonstrateExplicitWait(driver);
    }

    public static void demonstrateExplicitWait(WebDriver driver) {
        // Create an instance of WebDriverWait with a timeout of 10 seconds
        WebDriverWait wait = new WebDriverWait(driver, 10);

        // Wait for an element to be visible and then interact with it
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));
        element.click();

        // Wait for an element to be clickable and then click it
        WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("buttonID")));
        button.click();

        // Wait for the presence of an element located by XPath
        WebElement dynamicElement = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='dynamic']")));
        System.out.println(dynamicElement.getText());

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

				
			

Explanation

  • Import Required Packages: Import all necessary Selenium and Java packages.
  • Set Up WebDriver: Initialize the ChromeDriver and navigate to a website.
  • WebDriverWait: Create an instance of WebDriverWait with a specified timeout (10 seconds in this case).
  • ExpectedConditions: Use various methods from ExpectedConditions to wait for specific conditions:
    • visibilityOfElementLocated: Waits until the specified element is visible on the page.
    • elementToBeClickable: Waits until the specified element is clickable.
    • presenceOfElementLocated: Waits until the specified element is present in the DOM.

Detailed Conditions

Here are some commonly used ExpectedConditions:

  • visibilityOfElementLocated(By locator): Waits until the element specified by the locator is visible.
  • elementToBeClickable(By locator): Waits until the element specified by the locator is clickable.
  • presenceOfElementLocated(By locator): Waits until the element specified by the locator is present in the DOM.
  • textToBePresentInElement(By locator, String text): Waits until the specified text is present in the specified element.
  • titleContains(String title): Waits until the page title contains the specified text.

Explicit waits are a powerful tool for handling dynamic content on web pages, ensuring your tests are robust and reliable by synchronizing the execution flow with the state of the web elements.

Scroll to Top