By Shivam Singh, founder of TesterRank · Updated September 2026
Built-in conditions cover elements, URLs, titles and alerts. Real apps often need something more specific: "wait until the account balance changes", "wait until the table has at least 5 rows" or "wait until a spinner count reaches zero". A custom ExpectedCondition is just a small function that returns a value (or true) when your condition is met, and null (or false) while it is not.
null or false to say "not ready yet, keep waiting".Built-in ExpectedConditions cover standard scenarios, but modern web applications often have custom business conditions: waiting for an element attribute to change (e.g. class="step completed"), waiting for a CSS width animation to reach 100%, or waiting for JavaScript document.readyState to be complete. In Selenium 4, you can write custom conditions using clean Java lambda expressions.
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(15));
// 1. Custom lambda: Wait for CSS width property to hit 100%
wait.until(d -> {
WebElement bar = d.findElement(By.id("progress-fill"));
return "100%".equals(bar.getCssValue("width"));
});
// 2. Custom lambda: Wait for JavaScript document readyState to be 'complete'
wait.until(d -> {
JavascriptExecutor js = (JavascriptExecutor) d;
return "complete".equals(js.executeScript("return document.readyState"));
});
// 3. Custom lambda: Wait until table row count exceeds previous baseline
int baselineCount = 5;
wait.until(d -> d.findElements(By.cssSelector("table#records tbody tr")).size() > baselineCount);When the built-in conditions are not enough, write your own. This class collects a few custom waits in one place so every test can reuse them.
package com.testerrank.waits;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
public class CustomWaits {
public static ExpectedCondition<Boolean> attributeContains(By locator, String attribute, String value) {
return new ExpectedCondition<Boolean>() {
private String actualValue = "";
@Override
public Boolean apply(WebDriver driver) {
try {
actualValue = driver.findElement(locator).getAttribute(attribute);
return actualValue != null && actualValue.contains(value);
} catch (Exception e) {
return false;
}
}
@Override
public String toString() {
return String.format("attribute '%s' to contain '%s'. Actual value: '%s'", attribute, value, actualValue);
}
};
}
}Put custom conditions in one helper class so the whole team can reuse them. Give them clear names such as textToChangeFrom or rowCountAtLeast. Good names make the test read like plain English: "wait until the row count is at least 5".
Catch StaleElementReferenceException inside your condition and return null. Pages often re-render while you are waiting, and an uncaught exception ends the wait early with a confusing error.
Key Point: When built-in conditions fall short, pass custom Java lambdas to wait.until() to evaluate arbitrary browser and DOM states.