29. Dynamic Auto-Suggest & Search Autocomplete

Handling Dynamic Suggestions

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

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.
πŸ’‘Common doubts
Did you like the problem?
Lesson 29 of 44
Java
Loading code editor…
Next