We use cookies to analyze site traffic and show personalized ads. You can accept all cookies or decline personalized advertising.
Learn more in our Privacy Policy.
Master the built-in Expected Conditions for common wait scenarios
Expected Conditions are a set of predefined conditions used with explicit waits (WebDriver Wait and FluentWait).
Instead of writing a custom lambda function every time, you can reuse these built-in conditions for common scenarios like: waiting for visibility, clickability, text changes, and more.
presenceOfElementLocated(By locator)
Waits until an element is present in the DOM (may not be visible). Returns the WebElement.
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("btn")));presenceOfAllElementsLocatedBy(By locator)
Waits until at least one element matching the locator is present. Returns List<WebElement>.
List<WebElement> items = wait.until(
ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("item"))
);visibilityOfElementLocated(By locator)
Waits until an element is present AND visible (has height/width, not display:none). Returns the WebElement.
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("modal")));visibilityOf(WebElement element)
Similar to above but takes an existing WebElement instead of a locator.
WebElement el = driver.findElement(By.id("box"));
wait.until(ExpectedConditions.visibilityOf(el));invisibilityOfElementLocated(By locator)
Waits until an element is not visible or not present. Useful for waiting for loaders/spinners to disappear.
wait.until(ExpectedConditions.invisibilityOfElementLocated(
By.className("loading-spinner")
));elementToBeClickable(By locator)
Waits until an element is visible AND enabled (ready to be clicked). Most commonly used condition!
WebElement btn = wait.until(
ExpectedConditions.elementToBeClickable(By.id("submit-btn"))
);
btn.click();elementToBeSelected(WebElement element)
Waits until an element (checkbox, radio button, option) is selected.
wait.until(ExpectedConditions.elementToBeSelected(checkbox));textToBePresentInElementLocated(By locator, String text)
Waits until the visible text of an element contains the specified text (case-sensitive).
wait.until(ExpectedConditions.textToBePresentInElementLocated(
By.id("status"),
"Success"
));attributeToBe(By locator, String attribute, String value)
Waits until an element's specific attribute has the expected value.
wait.until(ExpectedConditions.attributeToBe(
By.id("input"),
"value",
"Expected Value"
));attributeContains(By locator, String attribute, String value)
Waits until an attribute contains the specified value (useful for class names).
wait.until(ExpectedConditions.attributeContains(
By.id("btn"),
"class",
"active"
));alertIsPresent()
Waits until a JavaScript alert is present. Returns the Alert object.
Alert alert = wait.until(ExpectedConditions.alertIsPresent());
alert.accept();numberOfWindowsToBe(int number)
Waits until the number of browser windows equals the specified count.
wait.until(ExpectedConditions.numberOfWindowsToBe(2));titleIs(String title)
Waits until the page title exactly matches the specified string.
wait.until(ExpectedConditions.titleIs("Dashboard - MyApp"));titleContains(String title)
Waits until the page title contains the specified text.
wait.until(ExpectedConditions.titleContains("Dashboard"));frameToBeAvailableAndSwitchToIt(By locator)
Waits until a frame is available and automatically switches to it.
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(
By.id("iframe")
));stalenessOf(WebElement element)
Waits until an element is no longer attached to the DOM. Useful after page reloads or dynamic content updates.
WebElement oldElement = driver.findElement(By.id("content"));
driver.navigate().refresh();
wait.until(ExpectedConditions.stalenessOf(oldElement));and(ExpectedCondition... conditions)
Waits until all specified conditions are true.
wait.until(ExpectedConditions.and(
ExpectedConditions.visibilityOfElementLocated(By.id("modal")),
ExpectedConditions.elementToBeClickable(By.id("submit"))
));or(ExpectedCondition... conditions)
Waits until at least one of the specified conditions is true.
wait.until(ExpectedConditions.or(
ExpectedConditions.titleContains("Success"),
ExpectedConditions.titleContains("Error")
));not(ExpectedCondition condition)
Waits until the specified condition is false.
wait.until(ExpectedConditions.not(
ExpectedConditions.attributeContains(By.id("btn"), "class", "disabled")
));| Use Case | Expected Condition |
|---|---|
| Wait for element to exist in DOM | presenceOfElementLocated |
| Wait for element to be visible | visibilityOfElementLocated |
| Wait for element to be clickable | elementToBeClickable |
| Wait for element to disappear | invisibilityOfElementLocated |
| Wait for text to appear | textToBePresentInElementLocated |
| Wait for alert | alertIsPresent |
| Wait for page title | titleContains / titleIs |
| Wait for attribute value | attributeToBe / attributeContains |
Use Expected Conditions instead of inline lambdas for common wait scenarios. They're tested, optimized, and make your code more readable.
Use visibilityOf for elements you'll interact with.presenceOf only checks DOM existence, not visibility.
Use and(), or(), and not() to create complex wait logic without writing custom conditions.