0
0
Selenium Javatesting~20 mins

Auto-complete field handling in Selenium Java - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Auto-complete 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 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();
ASelected: Apple
BNo output, throws NoSuchElementException
CSelected: App
DSelected: Apple Pie
Attempts:
2 left
💡 Hint
Look at the loop and the condition inside it. The code clicks the option with exact text "Apple".
locator
intermediate
1: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:
<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?
ABy.className("li")
BBy.cssSelector("ul.suggestions li")
CBy.xpath("//ul[@class='suggestions']/li")
DBy.id("suggestions")
Attempts:
2 left
💡 Hint
Look for a locator that selects all list items inside the suggestions list by class.
assertion
advanced
1: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");
AassertNotNull(value);
BassertTrue(value == "Banana");
CassertFalse(value.equals("Banana"));
DassertEquals("Banana", value);
Attempts:
2 left
💡 Hint
Use the correct JUnit assertion to compare string values for equality.
🔧 Debug
advanced
2:00remaining
Why does this auto-complete selection code fail with ElementNotInteractableException?
Given this code snippet:
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?
AThe input field is disabled, so click fails.
BThe locator "ul#list li" is incorrect and finds no elements.
CThe suggestions list is not visible yet when click is attempted.
DThe first option is already selected, so click is redundant.
Attempts:
2 left
💡 Hint
Think about timing and visibility of elements before clicking.
framework
expert
2: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?
A
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.suggestions li")));
List&lt;WebElement&gt; options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
    if (option.getText().equals("Orange")) {
        option.click();
        break;
    }
}
B
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("Orange")));
WebElement option = driver.findElement(By.id("Orange"));
option.click();
C
Thread.sleep(5000);
WebElement option = driver.findElement(By.xpath("//li[text()='Orange']"));
option.click();
D
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//ul[@class='suggestions']/li[contains(text(),'Orange')]");
WebElement option = driver.findElement(By.xpath("//li[text()='Orange']"));
option.click();
Attempts:
2 left
💡 Hint
Use WebDriverWait with Duration and check visibility of suggestions before clicking.