Explicit Wait is a more flexible wait mechanism in Selenium WebDriver that allows waiting for specific conditions before interacting with an element. Unlike Implicit Wait, which applies globally, Explicit Wait is applied only to the elements that need it.


✅ Syntax for Explicit Wait in Selenium

Explicit Wait is used for a specific element until a certain condition is met.

// Create WebDriverWait object with 10 seconds timeout
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

// Wait until the element is visible
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementID")));
element.click();

πŸ”Ή Explanation:

  • It waits up to 10 seconds for the element to be visible.
  • If the condition is met before 10 seconds, it proceeds immediately.
  • It only applies to one specific element.

πŸ”Ή How Does Explicit Wait Work?

  • It waits for a condition to be met before proceeding with the next step.
  • If the condition is met before the timeout, execution continues immediately.
  • If the condition is not met within the given time, a TimeoutException is thrown.

πŸ“Œ Example of Explicit Wait in Java

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) {
        WebDriver driver = new ChromeDriver();
        driver.get("https://example.com");

        // Creating Explicit Wait instance
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

        // Waiting for the element to be visible
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitButton")));
        
        // Performing action after the element is visible
        element.click();
        
        driver.quit();
    }
}

πŸ”Ή Expected Conditions in Explicit Wait

Selenium provides various predefined conditions in the ExpectedConditions class, which can be used with Explicit Wait to synchronize test execution. Below is the complete list of these conditions:


πŸ“Œ Visibility and Presence Conditions

Expected Condition
Description
visibilityOfElementLocated(By locator)
Waits until the element is visible on the page.
visibilityOf(WebElement element)
Waits until the specified WebElement is visible.
presenceOfElementLocated(By locator)
Waits until the element is present in the DOM (may be hidden).
presenceOfAllElementsLocatedBy(By locator)
Waits for all elements matching the locator to be present in the DOM.
invisibilityOfElementLocated(By locator)
Waits until the element disappears from the page.
invisibilityOf(WebElement element)
Waits until the specified WebElement is not visible.

Example:

WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("loginButton")));

πŸ“Œ Element Clickability and Interactivity

Expected Condition
Description
elementToBeClickable(By locator)
Waits until the element is visible and enabled to be clicked.
elementToBeClickable(WebElement element)
Waits until the specified WebElement is clickable.
elementToBeSelected(By locator)
Waits until the element is selected (useful for radio buttons, checkboxes).
elementToBeSelected(WebElement element)
Waits until the specified WebElement is selected.

Example:

WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
button.click();

πŸ“Œ Text and Attribute Conditions

Expected Condition
Description
textToBePresentInElementLocated(By locator, String text)
Waits until the specified text appears in the element.
textToBePresentInElement(WebElement element, String text)
Waits until the specified WebElement contains the text.
textToBePresentInElementValue(By locator, String text)
Waits until the input field value contains the specified text.
attributeToBe(WebElement element, String attribute, String value)
Waits until the specified attribute of an element equals the given value.

Example:

wait.until(ExpectedConditions.textToBePresentInElementLocated(By.id("statusMessage"), "Success"));

πŸ“Œ Frame and Window Conditions

Expected Condition
Description
frameToBeAvailableAndSwitchToIt(By locator)
Waits until the frame is available and switches to it.
frameToBeAvailableAndSwitchToIt(WebElement frameElement)
Waits for the specified WebElement (frame) to be available and switches to it.
numberOfWindowsToBe(int expectedWindows)
Waits until the specified number of windows/tabs are open.
titleIs(String title)
Waits until the page title is exactly as specified.
titleContains(String keyword)
Waits until the page title contains the given keyword.

Example:

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("iframe1")));

πŸ“Œ Alert Handling Conditions

Expected Condition Description
alertIsPresent() Waits until an alert pop-up is displayed.

Example:

wait.until(ExpectedConditions.alertIsPresent()).accept();

πŸ“Œ Miscellaneous Conditions

Expected Condition
Description
stalenessOf(WebElement element)
Waits until an element is removed from the DOM.
elementSelectionStateToBe(WebElement element, boolean state)
Waits until the selection state of an element matches the given boolean value.
elementSelectionStateToBe(By locator, boolean state)
Waits until the selection state of an element located by By matches the given boolean.

Example:

wait.until(ExpectedConditions.stalenessOf(oldElement));

πŸ“Œ Example: Using Multiple Expected Conditions

You can combine multiple conditions in a single wait statement using or() or and().

Example: Wait until either of two conditions is met

wait.until(ExpectedConditions.or(
    ExpectedConditions.visibilityOfElementLocated(By.id("loginButton")),
    ExpectedConditions.elementToBeClickable(By.id("signUpButton"))
));

✅ When to Use Explicit Wait?

  • When dealing with dynamically loaded elements (AJAX, JavaScript updates).
  • When elements take variable time to appear (e.g., slow network conditions).
  • When waiting for specific conditions (visibility, clickability, text update, etc.).

⚠️ Limitations of Explicit Wait

  • Needs to be applied separately for each element.
  • Slightly more complex compared to Implicit Wait.
  • If the expected condition is never met, it throws a TimeoutException.

πŸ”Ή Complete List of Expected Conditions with Return type in Explicit Wait

The methods you've listed are part of the ExpectedConditions class in Selenium's WebDriverWait mechanism. They are used for creating various conditions for waiting until certain elements or page states are met before proceeding further in test automation scripts.

Here's a breakdown of the return types for each method:

  1. alertIsPresent()

    • Return TypeAlert
    • Description: Waits until an alert box is present and returns a reference to it.
  2. elementSelectionStateToBe(WebElement element, boolean selected)

    • Return TypeBoolean
    • Description: Waits until the selection state of a given element is the specified state (selected or not selected).
  3. elementToBeClickable(By locator)

    • Return TypeWebElement
    • Description: Waits until the element is visible and enabled such that it is clickable.
  4. elementToBeSelected(By locator)

    • Return TypeBoolean
    • Description: Waits until the specified element is selected.
  5. frameToBeAvailableAndSwitchToIt(By locator)

    • Return TypeWebDriver
    • Description: Waits until the frame is available and then switches the WebDriver's context to it.
  6. invisibilityOfTheElementLocated(By locator)

    • Return TypeBoolean
    • Description: Waits until the specified element is no longer visible.
  7. invisibilityOfElementWithText(By locator, String text)

    • Return TypeBoolean
    • Description: Waits until the specified element is invisible or does not contain the given text.
  8. presenceOfAllElementsLocatedBy(By locator)

    • Return TypeList<WebElement>
    • Description: Waits until all elements matching the given locator are present in the DOM.
  9. presenceOfElementLocated(By locator)

    • Return TypeWebElement
    • Description: Waits until at least one element matching the given locator is present in the DOM.
  10. textToBePresentInElement(WebElement element, String text)

    • Return TypeBoolean
    • Description: Waits until the given text is present in the specified element.
  11. textToBePresentInElementLocated(By locator, String text)

    • Return TypeBoolean
    • Description: Waits until the specified text is present in the element located by the given locator.
  12. textToBePresentInElementValue(By locator, String text)

    • Return TypeBoolean
    • Description: Waits until the specified text is present in the element's value attribute.
  13. titleIs(String title)

    • Return TypeBoolean
    • Description: Waits until the page title matches the given title.
  14. titleContains(String title)

    • Return TypeBoolean
    • Description: Waits until the page title contains the given text.
  15. visibilityOf(WebElement element)

    • Return TypeWebElement
    • Description: Waits until the specified element is visible.
  16. visibilityOfAllElements(List<WebElement> elements)

    • Return TypeList<WebElement>
    • Description: Waits until all elements in the given list are visible.
  17. visibilityOfAllElementsLocatedBy(By locator)

    • Return TypeList<WebElement>
    • Description: Waits until all elements matching the given locator are visible.
  18. visibilityOfElementLocated(By locator)

    • Return TypeWebElement
    • Description: Waits until the element located by the given locator is visible.

 


πŸš€ Best Practices

  • Always use specific conditions instead of long wait times.
  • Avoid Thread.sleep() as it unnecessarily delays execution.
  • Do not mix Implicit and Explicit Wait, as it may lead to unexpected behavior.
  • Use try-catch blocks to handle TimeoutException when an element does not appear.