An explicit wait tells WebDriver to wait for a certain condition to happen before continuing. It waits for a specific element or condition for a set time.
Click to reveal answer
beginner
How do you create an explicit wait using WebDriverWait in Java?
You create a WebDriverWait object with the driver and timeout, then use it with ExpectedConditions to wait for a condition, like this:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("elementId")));
Click to reveal answer
intermediate
Why is explicit wait better than Thread.sleep()?
Explicit wait waits only as long as needed for a condition, making tests faster and more reliable. Thread.sleep() always waits the fixed time, which can slow tests and cause failures.
Click to reveal answer
intermediate
What happens if the condition in WebDriverWait is not met within the timeout?
If the condition is not met in the given time, WebDriverWait throws a TimeoutException, which means the test will fail at that point.
Click to reveal answer
beginner
Give an example of waiting for an element to be clickable using explicit wait.
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
Step 1: Understand the wait condition and timeout
The wait will pause up to 10 seconds for the button to become clickable.
Step 2: Analyze the element state after 5 seconds
The button becomes clickable after 5 seconds, so wait.until returns the element successfully.
Final Answer:
The test waits up to 10 seconds, finds the button when clickable, then clicks it -> Option A
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
Step 1: Check the WebDriverWait constructor usage
Since Selenium 4, the timeout must be a Duration object, not an int.
Step 2: Confirm the correct way to specify timeout
Use Duration.ofSeconds(10) to specify 10 seconds timeout.
Final Answer:
Timeout value should be wrapped in Duration.ofSeconds() -> Option B
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
Step 1: Use try-catch to handle timeout without failing test
Wrap the wait in try block and catch TimeoutException to log and continue.
Step 2: Use elementToBeClickable with correct selector and timeout
Create WebDriverWait with 5 seconds timeout and wait for clickable condition.
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.
Final Answer:
Code with try-catch around wait and logging on timeout -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.