Challenge - 5 Problems
Auto-complete Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this Selenium Java code snippet?
Consider the following Selenium Java code that tries to select an option from an auto-complete field. What will be printed to the console?
Selenium Java
WebDriver driver = new ChromeDriver(); driver.get("https://example.com/autocomplete"); WebElement input = driver.findElement(By.id("searchBox")); input.sendKeys("App"); Thread.sleep(1000); // wait for suggestions List<WebElement> options = driver.findElements(By.cssSelector("ul.suggestions li")); for (WebElement option : options) { if (option.getText().equals("Apple")) { option.click(); System.out.println("Selected: " + option.getText()); break; } } driver.quit();
Attempts:
2 left
💡 Hint
Look at the loop and the condition inside it. The code clicks the option with exact text "Apple".
✗ Incorrect
The code waits for suggestions, then loops through them. When it finds the option with text exactly "Apple", it clicks it and prints "Selected: Apple". Other options do not match exactly, so they are skipped.
❓ locator
intermediate1:30remaining
Which locator is best to select all auto-complete suggestions?
You want to locate all suggestion items in an auto-complete dropdown. The HTML structure is:
Which locator will correctly find all <li> elements inside the suggestions list?
<ul class="suggestions"> <li>Apple</li> <li>Apricot</li> <li>Avocado</li> </ul>
Which locator will correctly find all <li> elements inside the suggestions list?
Attempts:
2 left
💡 Hint
Look for a locator that selects all list items inside the suggestions list by class.
✗ Incorrect
Option B uses a CSS selector to find all <li> elements inside a <ul> with class "suggestions". Option B looks for an element with id "suggestions" which does not exist. Option B is valid XPath but less preferred than CSS selectors for simplicity. Option B looks for elements with class "li" which is incorrect because "li" is a tag, not a class.
❓ assertion
advanced1:30remaining
Which assertion correctly verifies the selected auto-complete value?
After selecting "Banana" from an auto-complete field, you want to assert the input field's value is "Banana". Which assertion is correct in Java with JUnit?
Selenium Java
WebElement input = driver.findElement(By.id("fruitInput")); String value = input.getAttribute("value");
Attempts:
2 left
💡 Hint
Use the correct JUnit assertion to compare string values for equality.
✗ Incorrect
Option D correctly uses assertEquals to compare the expected string "Banana" with the actual value. Option D uses == which compares references, not string content, so it can fail. Option D asserts the opposite of what is needed. Option D only checks that value is not null, not that it equals "Banana".
🔧 Debug
advanced2:00remaining
Why does this auto-complete selection code fail with ElementNotInteractableException?
Given this code snippet:
The test fails with ElementNotInteractableException on the click line. What is the most likely reason?
WebElement input = driver.findElement(By.id("search"));
input.sendKeys("Che");
List options = driver.findElements(By.cssSelector("ul#list li"));
options.get(0).click(); The test fails with ElementNotInteractableException on the click line. What is the most likely reason?
Attempts:
2 left
💡 Hint
Think about timing and visibility of elements before clicking.
✗ Incorrect
ElementNotInteractableException usually means the element is present in the DOM but not visible or not ready for interaction. Here, the suggestions list likely appears with delay after typing, so clicking immediately fails. Option C would cause NoSuchElementException, not this error. Option C is unrelated to the clicked element. Option C does not cause this exception.
❓ framework
expert2:30remaining
Which Selenium Java code snippet correctly waits for auto-complete suggestions to appear before selecting "Orange"?
You want to wait up to 5 seconds for the auto-complete suggestions to appear, then select the option "Orange". Which code snippet correctly implements this using WebDriverWait and ExpectedConditions?
Attempts:
2 left
💡 Hint
Use WebDriverWait with Duration and check visibility of suggestions before clicking.
✗ Incorrect
Option A correctly creates a WebDriverWait with Duration.ofSeconds(5), waits for visibility of any suggestion item, then loops to find and click "Orange". Option A uses deprecated constructor and wrong locator. Option A uses Thread.sleep which is not recommended. Option A has a syntax error (missing closing parenthesis in wait.until).