Auto-complete fields help users by suggesting options as they type. Testing them ensures the suggestions work correctly and the right option can be selected.
Auto-complete field handling in Selenium Java
WebElement input = driver.findElement(By.id("inputFieldId")); input.sendKeys("partial text"); // Wait for suggestions to appear WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".suggestion-class"))); // Select suggestion List<WebElement> suggestions = driver.findElements(By.cssSelector(".suggestion-class")); for (WebElement suggestion : suggestions) { if (suggestion.getText().equals("Expected Option")) { suggestion.click(); break; } }
Use explicit waits to wait for suggestions to load before interacting.
Use unique and stable locators for the input and suggestion elements.
input.sendKeys("New"); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".autocomplete-item"))); List<WebElement> items = driver.findElements(By.cssSelector(".autocomplete-item")); items.get(0).click();
input.sendKeys("San"); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".suggestion"))); for (WebElement item : driver.findElements(By.cssSelector(".suggestion"))) { if (item.getText().contains("San Francisco")) { item.click(); break; } }
This test opens a page with an auto-complete field, types 'Lon', waits for suggestions, selects 'London', and verifies the selection.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.List; public class AutoCompleteTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/autocomplete"); WebElement input = driver.findElement(By.id("city-input")); input.sendKeys("Lon"); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".autocomplete-suggestion"))); List<WebElement> suggestions = driver.findElements(By.cssSelector(".autocomplete-suggestion")); boolean found = false; for (WebElement suggestion : suggestions) { if (suggestion.getText().equals("London")) { suggestion.click(); found = true; break; } } if (found && input.getAttribute("value").equals("London")) { System.out.println("Test Passed: 'London' selected successfully."); } else { System.out.println("Test Failed: 'London' not selected."); } } finally { driver.quit(); } } }
Always use explicit waits to avoid flaky tests when waiting for suggestions.
Check that the input field's value updates after clicking a suggestion.
Use meaningful locators that are less likely to change with UI updates.
Auto-complete fields suggest options as users type.
Use explicit waits to handle dynamic suggestion lists.
Select the correct suggestion by matching text and verify the input value.
Practice
Solution
Step 1: Understand auto-complete behavior
Auto-complete fields show suggestions dynamically as the user types.Step 2: Purpose of handling in Selenium
We need to select the correct suggestion from the list to simulate user interaction accurately.Final Answer:
To select the correct suggestion from the dynamic list based on user input -> Option DQuick Check:
Auto-complete handling = selecting suggestion [OK]
- Trying to disable auto-complete instead of selecting
- Ignoring dynamic suggestion list
- Submitting form without input
Solution
Step 1: Identify correct wait usage
Explicit wait with WebDriverWait and ExpectedConditions waits for element visibility properly.Step 2: Compare with other options
driver.findElement(By.id("suggestions")).click(); clicks without waiting, C uses Thread.sleep which is not best practice, D uses implicit wait which is less reliable for dynamic elements.Final Answer:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("suggestions"))); -> Option AQuick Check:
Explicit wait for visibility = new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.id("suggestions"))); [OK]
- Using Thread.sleep instead of explicit wait
- Relying only on implicit waits
- Clicking before element is visible
driver.findElement(By.id("searchBox")).sendKeys("App");
new WebDriverWait(driver, Duration.ofSeconds(5))
.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("ul.suggestions li")));
List<WebElement> options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
if (option.getText().equals("Apple")) {
option.click();
break;
}
}
String value = driver.findElement(By.id("searchBox")).getAttribute("value");Solution
Step 1: Analyze input and wait
The code types "App" and waits for suggestions to appear.Step 2: Select matching suggestion
It loops through suggestions and clicks the one with text "Apple".Step 3: Check input value after click
Clicking "Apple" updates the input field value to "Apple".Final Answer:
"Apple" -> Option BQuick Check:
Selected suggestion text = input value [OK]
- Assuming input stays as typed text
- Ignoring wait for suggestions
- Expecting empty input after click
driver.findElement(By.id("searchBox")).sendKeys("Ban");
List<WebElement> options = driver.findElements(By.cssSelector("ul.suggestions li"));
for (WebElement option : options) {
if (option.getText().equals("Banana")) {
option.click();
}
}Solution
Step 1: Check for wait usage
The code finds suggestions immediately after typing without waiting for them to appear.Step 2: Consequence of missing wait
Without explicit wait, options list may be empty causing no click action.Final Answer:
Missing explicit wait for suggestions to appear before finding elements -> Option AQuick Check:
Wait before findElements = Missing explicit wait for suggestions to appear before finding elements [OK]
- Skipping explicit wait causing empty list
- Assuming suggestions load instantly
- Not breaking loop after click
ul with class "autocomplete-list" and each suggestion is a li. Which approach correctly handles this scenario including waiting and verification?Solution
Step 1: Send partial keys and wait explicitly
Typing "Che" triggers suggestions; explicit wait ensures suggestions are visible before interaction.Step 2: Find and click correct suggestion, then verify
Find alllielements, click the one with text "Cherry", then assert input value matches to confirm selection.Final Answer:
Send keys "Che" to input, wait explicitly for visibility oful.autocomplete-list, find allli, click the one with text "Cherry", then assert input value equals "Cherry" -> Option CQuick Check:
Explicit wait + correct click + assert input value = Send keys "Che" to input, wait explicitly for visibility oful.autocomplete-list, find allli, click the one with text "Cherry", then assert input value equals "Cherry" [OK]
- Not waiting explicitly for suggestions
- Clicking wrong suggestion or without verification
- Using Thread.sleep instead of explicit wait
Ready to prove it?
Pass the test and this pattern moves unknown
