Bird
Raised Fist0
Selenium Javatesting~20 mins

ExpectedConditions class in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
ExpectedConditions Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of waiting for element visibility
What will be the result of this Selenium Java code snippet when the element with id 'submitBtn' becomes visible within 10 seconds?
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitBtn")));
System.out.println(element.isDisplayed());
ANoSuchElementException
Btrue
Cfalse
DTimeoutException
Attempts:
2 left
💡 Hint
visibilityOfElementLocated waits until the element is visible on the page.
❓ assertion
intermediate
2:00remaining
Correct assertion for element to be clickable
Which assertion correctly verifies that a button with id 'loginBtn' is clickable using ExpectedConditions in Selenium Java?
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn")));
AassertTrue(button.isEnabled() && button.isDisplayed());
BassertFalse(button.isEnabled());
CassertNull(button);
DassertEquals(button.getText(), "Submit");
Attempts:
2 left
💡 Hint
elementToBeClickable means the element is visible and enabled.
🔧 Debug
advanced
2:00remaining
Identify the error in ExpectedConditions usage
What is the error in this Selenium Java code snippet?
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(8));
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[@class='content']")));
ANo error; code waits for element presence correctly.
BBy.xpath locator syntax is invalid.
CDuration.ofSeconds should be replaced with just seconds as int.
DpresenceOfElementLocated does not wait for visibility, so element may not be interactable.
Attempts:
2 left
💡 Hint
presenceOfElementLocated waits only for element in DOM, not visibility.
🧠 Conceptual
advanced
2:00remaining
Understanding ExpectedConditions stalenessOf
What does the ExpectedConditions.stalenessOf(WebElement element) condition check in Selenium Java?
AChecks if the element is no longer attached to the DOM.
BChecks if the element is visible on the page.
CChecks if the element is enabled and clickable.
DChecks if the element's text matches a given string.
Attempts:
2 left
💡 Hint
Staleness means the element reference is outdated.
❓ framework
expert
3:00remaining
Best practice for waiting for multiple conditions
In Selenium Java, which approach correctly waits until both an element with id 'username' is visible and a button with id 'submit' is clickable before proceeding?
A
wait.until(ExpectedConditions.and(
    ExpectedConditions.visibilityOfElementLocated(By.id("username")),
    ExpectedConditions.elementToBeClickable(By.id("submit"))
));
B
wait.until(ExpectedConditions.or(
    ExpectedConditions.visibilityOfElementLocated(By.id("username")),
    ExpectedConditions.elementToBeClickable(By.id("submit"))
));
C
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("submit")));
D
wait.until(ExpectedConditions.not(
    ExpectedConditions.invisibilityOfElementLocated(By.id("username"))
));
Attempts:
2 left
💡 Hint
Chain sequential wait.until calls to ensure both conditions.

Practice

(1/5)
1. What is the main purpose of the ExpectedConditions class in Selenium WebDriver?
easy
A. To perform browser navigation actions
B. To locate web elements on the page
C. To wait for certain conditions or states of web elements before proceeding
D. To execute JavaScript code on the page

Solution

  1. Step 1: Understand the role of ExpectedConditions

    The ExpectedConditions class provides predefined conditions to wait for, such as visibility or clickability of elements.
  2. Step 2: Compare with other Selenium functions

    Locating elements, navigation, and JavaScript execution are handled by other classes or methods, not ExpectedConditions.
  3. Final Answer:

    To wait for certain conditions or states of web elements before proceeding -> Option C
  4. Quick Check:

    ExpectedConditions = wait for element states [OK]
Hint: ExpectedConditions waits for element states before actions [OK]
Common Mistakes:
  • Confusing ExpectedConditions with element locators
  • Thinking it performs actions like clicking
  • Assuming it runs JavaScript
2. Which of the following is the correct syntax to wait until an element located by By.id("submit") is clickable using ExpectedConditions and WebDriverWait?
easy
A. WebDriverWait(driver, 10).wait(ExpectedConditions.elementToBeClickable(By.id("submit")));
B. new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit")));
C. new WebDriverWait(driver, 10).waitFor(ExpectedConditions.elementToBeClickable(By.id("submit")));
D. WebDriverWait(driver, 10).until(ExpectedConditions.elementIsClickable(By.id("submit")));

Solution

  1. Step 1: Identify correct WebDriverWait constructor and method

    The correct way is to create a new WebDriverWait object and call the until method.
  2. Step 2: Check ExpectedConditions method name

    The method is elementToBeClickable, not elementIsClickable, and the locator is passed correctly.
  3. Final Answer:

    new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("submit"))); -> Option B
  4. Quick Check:

    Use new WebDriverWait + until + elementToBeClickable [OK]
Hint: Use new WebDriverWait(driver, time).until(condition) [OK]
Common Mistakes:
  • Using wait() instead of until()
  • Wrong method name like elementIsClickable
  • Not creating new WebDriverWait instance
3. Consider the following code snippet:
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?
medium
A. The test passes without clicking the element
B. The code clicks on the element anyway
C. The wait silently ignores and continues without clicking
D. A TimeoutException is thrown and the test fails

Solution

  1. Step 1: Understand behavior of WebDriverWait with ExpectedConditions

    If the condition is not met within the timeout, WebDriverWait throws a TimeoutException.
  2. Step 2: Analyze the code flow

    Since the element is not visible within 5 seconds, wait.until throws an exception, so element.click() is not executed.
  3. Final Answer:

    A TimeoutException is thrown and the test fails -> Option D
  4. Quick Check:

    TimeoutException if condition unmet in wait [OK]
Hint: Wait throws TimeoutException if condition unmet [OK]
Common Mistakes:
  • Assuming click happens even if element not visible
  • Thinking wait ignores failure silently
  • Believing test passes without clicking
4. Identify the error in the following code snippet:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.presenceOfElementLocated("#username"));
medium
A. The locator should be a By object, not a String
B. The timeout value should be in milliseconds
C. ExpectedConditions.presenceOfElementLocated does not exist
D. WebDriverWait cannot be used with ExpectedConditions

Solution

  1. Step 1: Check the argument type for presenceOfElementLocated

    This method requires a By locator, not a plain string.
  2. Step 2: Verify the locator usage

    Instead of passing "#username", it should be By.cssSelector("#username") or another By method.
  3. Final Answer:

    The locator should be a By object, not a String -> Option A
  4. Quick Check:

    presenceOfElementLocated needs By locator [OK]
Hint: Use By locators, not strings, with ExpectedConditions [OK]
Common Mistakes:
  • Passing string selector instead of By object
  • Confusing timeout units (seconds vs milliseconds)
  • Thinking ExpectedConditions methods don't exist
5. You want to wait until either an element with id 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?
hard
A. new WebDriverWait(driver, 15).until(ExpectedConditions.invisibilityOfElementLocated(By.id("loading"))); driver.findElement(By.id("continue")).click();
B. new WebDriverWait(driver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.id("loading"))); driver.findElement(By.id("continue")).click();
C. new WebDriverWait(driver, 15).until(ExpectedConditions.elementToBeClickable(By.id("loading"))); driver.findElement(By.id("continue")).click();
D. new WebDriverWait(driver, 15).until(ExpectedConditions.presenceOfElementLocated(By.id("continue"))); driver.findElement(By.id("loading")).click();

Solution

  1. Step 1: Identify the condition to wait for

    The requirement is to wait until the element with id "loading" disappears, which matches invisibilityOfElementLocated.
  2. Step 2: Confirm the action after wait

    After waiting, the code clicks the button with id "continue" as required.
  3. Final Answer:

    new WebDriverWait(driver, 15).until(ExpectedConditions.invisibilityOfElementLocated(By.id("loading"))); driver.findElement(By.id("continue")).click(); -> Option A
  4. Quick Check:

    Wait for invisibility, then click continue [OK]
Hint: Wait for invisibilityOfElementLocated before clicking [OK]
Common Mistakes:
  • Waiting for visibility instead of invisibility
  • Clicking wrong element after wait
  • Using presenceOfElementLocated for disappearance
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard