Selenium + Java · Lesson 29 of 44
Module 29: Auto Complete & Dynamic Lists
Your task
Implement pickSuggestion:
Write it in the editor, then press Check answer (Ctrl + Enter).
WebElement searchInput = driver.findElement(By.name("q"));
searchInput.sendKeys("Playwright");
// Wait for suggestion list items to appear:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
List<WebElement> suggestions = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("ul.suggestions li"))
);
// Click matching option:
for (WebElement item : suggestions) {
if (item.getText().contains("Playwright Java")) {
item.click();
break;
}
}Google-style search inputs fetch suggestions asynchronously. In this lesson, you will type text into auto-complete inputs, wait for the suggestion panel to populate, and select the matching item. Let's master autocomplete!
Part of the Selenium + Java course (Selenium with Java). Practice this concept live in the Selenium Online Compiler or explore our free automation practice form.
Typing triggers an asynchronous AJAX call. You must use WebDriverWait to wait for visibility of the suggestions container.
Ready? Solve the task in the editor
Use Check answer to mark it complete. Hints are there if you need them.
___