Bird
Raised Fist0
Selenium Javatesting~20 mins

Explicit wait (WebDriverWait) in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Explicit Wait Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this explicit wait code snippet?
Consider the following Selenium Java code using WebDriverWait. What will be the result printed?
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("submitBtn")));
System.out.println(element.isDisplayed());
Atrue
Bfalse
CTimeoutException
DNoSuchElementException
Attempts:
2 left
💡 Hint
The wait waits until the element is visible or timeout occurs.
❓ assertion
intermediate
2:00remaining
Which assertion correctly verifies the element is clickable using explicit wait?
You want to assert that a button with id 'loginBtn' is clickable before clicking it. Which assertion is correct?
AassertNull(wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))));
BassertFalse(wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))).isDisplayed());
CassertTrue(wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))).isEnabled());
DassertEquals(wait.until(ExpectedConditions.elementToBeClickable(By.id("loginBtn"))), null);
Attempts:
2 left
💡 Hint
Clickable means the element is enabled and visible.
🔧 Debug
advanced
2:00remaining
Why does this explicit wait code throw TimeoutException?
Analyze the code below. Why does it throw TimeoutException even though the element exists?
Selenium Java
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("dynamicElement")));
// The element appears after 7 seconds on the page
AThe wait time is less than the element's appearance delay, so it times out.
BThe locator By.id("dynamicElement") is incorrect and cannot find the element.
CThe element is present but not clickable, causing TimeoutException.
DThe driver is not initialized, causing the wait to fail.
Attempts:
2 left
💡 Hint
Check the wait duration vs element appearance time.
🧠 Conceptual
advanced
2:00remaining
What is the main difference between implicit wait and explicit wait in Selenium?
Choose the correct statement that explains the key difference between implicit and explicit waits.
AImplicit wait is deprecated; explicit wait is the only recommended wait method.
BImplicit wait waits for a specific condition; explicit wait sets a default wait time for all elements.
CImplicit wait only works with JavaScript alerts; explicit wait only works with page loads.
DImplicit wait sets a default wait time for all element searches; explicit wait waits for a specific condition for a particular element.
Attempts:
2 left
💡 Hint
Think about scope and usage of each wait type.
❓ framework
expert
3:00remaining
Which code snippet correctly implements a fluent wait with polling and ignoring exceptions?
Select the code that creates a FluentWait in Selenium Java that polls every 2 seconds, waits up to 15 seconds, and ignores NoSuchElementException.
A
Wait<WebDriver> wait = new FluentWait<>(driver)
  .withTimeout(Duration.ofSeconds(15))
  .ignoring(NoSuchElementException.class)
  .pollingEvery(Duration.ofSeconds(2));
B
Wait<WebDriver> wait = new FluentWait<>(driver)
  .withTimeout(Duration.ofSeconds(15))
  .pollingEvery(Duration.ofSeconds(2))
  .ignoring(NoSuchElementException.class);
C
Wait<WebDriver> wait = new FluentWait<>(driver)
  .pollingEvery(Duration.ofSeconds(15))
  .withTimeout(Duration.ofSeconds(2))
  .ignoring(NoSuchElementException.class);
D
Wait<WebDriver> wait = new FluentWait<>(driver)
  .withTimeout(Duration.ofSeconds(2))
  .pollingEvery(Duration.ofSeconds(15))
  .ignoring(NoSuchElementException.class);
Attempts:
2 left
💡 Hint
Check the order and values of timeout and polling intervals.

Practice

(1/5)
1. What is the main purpose of using WebDriverWait in Selenium tests?
easy
A. To automatically retry failed tests
B. To speed up the test execution by skipping waits
C. To pause the test until a specific condition is met or timeout occurs
D. To capture screenshots during test execution

Solution

  1. Step 1: Understand the role of waits in Selenium

    Waits help tests handle dynamic page content by pausing until elements are ready.
  2. Step 2: Identify what WebDriverWait does

    WebDriverWait explicitly waits for a condition like element visibility or clickability before proceeding.
  3. Final Answer:

    To pause the test until a specific condition is met or timeout occurs -> Option C
  4. Quick Check:

    Explicit wait = pause until condition met [OK]
Hint: Waits pause tests until conditions, not speed up [OK]
Common Mistakes:
  • Confusing explicit wait with implicit wait
  • Thinking it speeds up tests by skipping steps
  • Using it to retry tests automatically
2. Which of the following is the correct syntax to create a WebDriverWait object with a 10-second timeout in Java?
easy
A. WebDriverWait wait = new WebDriverWait(driver, 10);
B. WebDriverWait wait = new WebDriverWait(Duration.ofSeconds(10), driver);
C. WebDriverWait wait = new WebDriverWait(10, driver);
D. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));

Solution

  1. Step 1: Recall the constructor for WebDriverWait in Selenium 4+

    It requires the WebDriver instance and a Duration object for timeout.
  2. Step 2: Match the correct syntax

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); uses Duration.ofSeconds(10) correctly with the driver as first argument.
  3. Final Answer:

    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); -> Option D
  4. Quick Check:

    Use Duration for timeout in WebDriverWait [OK]
Hint: Use Duration.ofSeconds for timeout in WebDriverWait [OK]
Common Mistakes:
  • Passing timeout as int instead of Duration
  • Swapping driver and timeout arguments
  • Using deprecated constructors
3. What will happen when the following code runs if the element with id "submitBtn" becomes clickable after 5 seconds?
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id("submitBtn")));
button.click();
medium
A. The test waits up to 10 seconds, finds the button when clickable, then clicks it
B. The test immediately throws a timeout exception
C. The test clicks the button immediately without waiting
D. The test waits 5 seconds then throws a stale element exception

Solution

  1. Step 1: Understand the wait condition and timeout

    The wait will pause up to 10 seconds for the button to become clickable.
  2. Step 2: Analyze the element state after 5 seconds

    The button becomes clickable after 5 seconds, so wait.until returns the element successfully.
  3. Final Answer:

    The test waits up to 10 seconds, finds the button when clickable, then clicks it -> Option A
  4. Quick Check:

    Waits up to timeout, proceeds when condition met [OK]
Hint: Waits up to timeout, proceeds once condition is true [OK]
Common Mistakes:
  • Assuming immediate failure if element not ready
  • Thinking wait always waits full timeout
  • Confusing stale element with clickable condition
4. Identify the error in this code snippet using WebDriverWait:
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));
medium
A. ExpectedConditions is not imported
B. Timeout value should be wrapped in Duration.ofSeconds()
C. By.id() cannot be used with visibilityOfElementLocated
D. wait.until() should return a boolean, not void

Solution

  1. Step 1: Check the WebDriverWait constructor usage

    Since Selenium 4, the timeout must be a Duration object, not an int.
  2. Step 2: Confirm the correct way to specify timeout

    Use Duration.ofSeconds(10) to specify 10 seconds timeout.
  3. Final Answer:

    Timeout value should be wrapped in Duration.ofSeconds() -> Option B
  4. Quick Check:

    Timeout must be Duration, not int [OK]
Hint: Use Duration.ofSeconds for timeout, not plain int [OK]
Common Mistakes:
  • Passing int instead of Duration causes compile error
  • Ignoring import errors for ExpectedConditions
  • Misunderstanding return type of wait.until()
5. You want to wait for a button with CSS class "submit-btn" to be clickable, but only wait up to 5 seconds. If it doesn't become clickable, you want to log a message and continue without failing the test. Which code snippet correctly implements this using WebDriverWait?
hard
A. try { WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".submit-btn"))); btn.click(); } catch (TimeoutException e) { System.out.println("Button not clickable in time, continuing test."); }
B. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); WebElement btn = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".submit-btn"))); btn.click(); System.out.println("Button not clickable in time, continuing test.");
C. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); if (wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".submit-btn")))) { driver.findElement(By.cssSelector(".submit-btn")).click(); } else { System.out.println("Button not clickable in time, continuing test."); }
D. WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); WebElement btn = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".submit-btn"))); btn.click();

Solution

  1. Step 1: Use try-catch to handle timeout without failing test

    Wrap the wait in try block and catch TimeoutException to log and continue.
  2. Step 2: Use elementToBeClickable with correct selector and timeout

    Create WebDriverWait with 5 seconds timeout and wait for clickable condition.
  3. Step 3: Click the button if found, else log message

    If wait succeeds, click the button; if timeout occurs, catch block logs message and test continues.
  4. Final Answer:

    Code with try-catch around wait and logging on timeout -> Option A
  5. Quick Check:

    Handle timeout with try-catch to continue test [OK]
Hint: Use try-catch around wait to handle timeout gracefully [OK]
Common Mistakes:
  • Not catching TimeoutException causing test failure
  • Using visibility instead of clickable condition
  • Logging message after wait without exception handling
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