The ExpectedConditions class helps wait for certain things to happen on a web page before continuing. This avoids errors when elements are not ready yet.
ExpectedConditions class in Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
wait.until(ExpectedConditions.condition(locator));Replace condition with the specific wait condition you want, like elementToBeClickable or visibilityOfElementLocated.
The locator is how you find the element, for example By.id("submit").
wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("message")));wait.until(ExpectedConditions.titleContains("Welcome"));This test opens a login page, waits for the username field to appear, enters text, waits for the login button to be clickable, clicks it, then waits for a welcome message to show and prints it.
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 WaitExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); driver.get("https://example.com/login"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); // Wait until the username field is visible WebElement username = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username"))); username.sendKeys("testuser"); // Wait until the login button is clickable WebElement loginButton = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))); loginButton.click(); // Wait until the welcome message appears WebElement welcomeMsg = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("welcomeMessage"))); System.out.println(welcomeMsg.getText()); driver.quit(); } }
Always use ExpectedConditions with WebDriverWait to avoid errors from elements not ready yet.
Set a reasonable timeout to avoid waiting too long or too short.
Use the right condition for your need, like visibility, clickability, or presence.
ExpectedConditions helps wait for web page elements or states before acting.
Use it with WebDriverWait and a locator to wait safely.
This avoids errors and makes tests more reliable.
Practice
ExpectedConditions class in Selenium WebDriver?Solution
Step 1: Understand the role of ExpectedConditions
The ExpectedConditions class provides predefined conditions to wait for, such as visibility or clickability of elements.Step 2: Compare with other Selenium functions
Locating elements, navigation, and JavaScript execution are handled by other classes or methods, not ExpectedConditions.Final Answer:
To wait for certain conditions or states of web elements before proceeding -> Option CQuick Check:
ExpectedConditions = wait for element states [OK]
- Confusing ExpectedConditions with element locators
- Thinking it performs actions like clicking
- Assuming it runs JavaScript
By.id("submit") is clickable using ExpectedConditions and WebDriverWait?Solution
Step 1: Identify correct WebDriverWait constructor and method
The correct way is to create a new WebDriverWait object and call theuntilmethod.Step 2: Check ExpectedConditions method name
The method iselementToBeClickable, notelementIsClickable, and the locator is passed correctly.Final Answer:
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); -> Option BQuick Check:
Use new WebDriverWait + until + elementToBeClickable [OK]
- Using wait() instead of until()
- Wrong method name like elementIsClickable
- Not creating new WebDriverWait instance
WebDriverWait wait = new WebDriverWait(driver, 5);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".login-button")));
element.click();What happens if the element with class
login-button is not visible within 5 seconds?Solution
Step 1: Understand behavior of WebDriverWait with ExpectedConditions
If the condition is not met within the timeout, WebDriverWait throws a TimeoutException.Step 2: Analyze the code flow
Since the element is not visible within 5 seconds,wait.untilthrows an exception, soelement.click()is not executed.Final Answer:
A TimeoutException is thrown and the test fails -> Option DQuick Check:
TimeoutException if condition unmet in wait [OK]
- Assuming click happens even if element not visible
- Thinking wait ignores failure silently
- Believing test passes without clicking
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated("#username"));Solution
Step 1: Check the argument type for presenceOfElementLocated
This method requires aBylocator, not a plain string.Step 2: Verify the locator usage
Instead of passing "#username", it should beBy.cssSelector("#username")or another By method.Final Answer:
The locator should be a By object, not a String -> Option AQuick Check:
presenceOfElementLocated needs By locator [OK]
- Passing string selector instead of By object
- Confusing timeout units (seconds vs milliseconds)
- Thinking ExpectedConditions methods don't exist
loading disappears or a maximum of 15 seconds passes before clicking a button with id continue. Which code snippet correctly uses ExpectedConditions to achieve this?Solution
Step 1: Identify the condition to wait for
The requirement is to wait until the element with id "loading" disappears, which matchesinvisibilityOfElementLocated.Step 2: Confirm the action after wait
After waiting, the code clicks the button with id "continue" as required.Final Answer:
new WebDriverWait(driver, 15).until(ExpectedConditions.invisibilityOfElementLocated(By.id("loading"))); driver.findElement(By.id("continue")).click(); -> Option AQuick Check:
Wait for invisibility, then click continue [OK]
- Waiting for visibility instead of invisibility
- Clicking wrong element after wait
- Using presenceOfElementLocated for disappearance
Ready to prove it?
Pass the test and this pattern moves unknown
