Selenium + Java · Lesson 29 of 44

0/44 done

Module 29: Auto Complete & Dynamic Lists

29. Dynamic Auto-Suggest & Search Autocomplete

15 minlesson

Your task

Implement pickSuggestion:

  1. Accept List<WebElement> suggestions and String keyword.
  2. Iterate and click the first suggestion where item.getText().contains(keyword). Return true if clicked.

Write it in the editor, then press Check answer (Ctrl + Enter).

Handling Dynamic Suggestions

java
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;
    }
}

Key Takeaways & Walkthrough Summary

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.

Common Doubts & Lesson Q&A

Why does finding suggestions immediately after sendKeys fail?

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.

Java
Skip