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 |
---|---|
|
Waits until the element is visible on the page. |
|
Waits until the specified WebElement is visible. |
|
Waits until the element is present in the DOM (may be hidden). |
|
Waits for all elements matching the locator to be present in the DOM. |
|
Waits until the element disappears from the page. |
|
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 |
---|---|
|
Waits until the element is visible and enabled to be clicked. |
|
Waits until the specified WebElement is clickable. |
|
Waits until the element is selected (useful for radio buttons, checkboxes). |
|
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 |
---|---|
|
Waits until the specified text appears in the element. |
|
Waits until the specified WebElement contains the text. |
|
Waits until the input field value contains the specified text. |
|
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 |
---|---|
|
Waits until the frame is available and switches to it. |
|
Waits for the specified WebElement (frame) to be available and switches to it. |
|
Waits until the specified number of windows/tabs are open. |
|
Waits until the page title is exactly as specified. |
|
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 |
---|---|
|
Waits until an element is removed from the DOM. |
|
Waits until the selection state of an element matches the given boolean value. |
|
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:
alertIsPresent()
- Return Type:
Alert
- Description: Waits until an alert box is present and returns a reference to it.
- Return Type:
elementSelectionStateToBe(WebElement element, boolean selected)
- Return Type:
Boolean
- Description: Waits until the selection state of a given element is the specified state (
selected
ornot selected
).
- Return Type:
elementToBeClickable(By locator)
- Return Type:
WebElement
- Description: Waits until the element is visible and enabled such that it is clickable.
- Return Type:
elementToBeSelected(By locator)
- Return Type:
Boolean
- Description: Waits until the specified element is selected.
- Return Type:
frameToBeAvailableAndSwitchToIt(By locator)
- Return Type:
WebDriver
- Description: Waits until the frame is available and then switches the WebDriver's context to it.
- Return Type:
invisibilityOfTheElementLocated(By locator)
- Return Type:
Boolean
- Description: Waits until the specified element is no longer visible.
- Return Type:
invisibilityOfElementWithText(By locator, String text)
- Return Type:
Boolean
- Description: Waits until the specified element is invisible or does not contain the given text.
- Return Type:
presenceOfAllElementsLocatedBy(By locator)
- Return Type:
List<WebElement>
- Description: Waits until all elements matching the given locator are present in the DOM.
- Return Type:
presenceOfElementLocated(By locator)
- Return Type:
WebElement
- Description: Waits until at least one element matching the given locator is present in the DOM.
- Return Type:
textToBePresentInElement(WebElement element, String text)
- Return Type:
Boolean
- Description: Waits until the given text is present in the specified element.
- Return Type:
textToBePresentInElementLocated(By locator, String text)
- Return Type:
Boolean
- Description: Waits until the specified text is present in the element located by the given locator.
- Return Type:
textToBePresentInElementValue(By locator, String text)
- Return Type:
Boolean
- Description: Waits until the specified text is present in the element's value attribute.
- Return Type:
titleIs(String title)
- Return Type:
Boolean
- Description: Waits until the page title matches the given title.
- Return Type:
titleContains(String title)
- Return Type:
Boolean
- Description: Waits until the page title contains the given text.
- Return Type:
visibilityOf(WebElement element)
- Return Type:
WebElement
- Description: Waits until the specified element is visible.
- Return Type:
visibilityOfAllElements(List<WebElement> elements)
- Return Type:
List<WebElement>
- Description: Waits until all elements in the given list are visible.
- Return Type:
visibilityOfAllElementsLocatedBy(By locator)
- Return Type:
List<WebElement>
- Description: Waits until all elements matching the given locator are visible.
- Return Type:
visibilityOfElementLocated(By locator)
- Return Type:
WebElement
- Description: Waits until the element located by the given locator is visible.
- Return Type:
π 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.