Type into the widget, wait for the intended suggestion to become available, select it using the widget’s supported interaction, and verify that the selection took effect. First check the page’s markup: Selenium’s Select class works with a native HTML <select>, not a custom autocomplete menu made from elements such as div or li.
Identify the widget before choosing an interaction
Autocomplete interfaces can look alike while having different underlying controls. Inspect the DOM and determine which case you have before writing the test.
- Native select: The control is an HTML
<select>containing<option>elements. Selenium’sSelectAPI is appropriate for this markup. See the Selenium guide to select list elements. - Custom autocomplete: The page has a text input and a separate suggestion list, commonly built with elements such as
divorli. Locate and interact with the actual suggestion or use the widget’s keyboard controls; do not wrap the input inSelect.
Also establish how results arrive and how the widget accepts a choice. Results may be added or refreshed asynchronously after typing. A visible row is not necessarily the accepted value, so your test should verify the resulting input value or another application-specific selected state.
Set up Selenium for Java
Use the Selenium Java dependency appropriate to your project’s build tool and version. The Selenium Project’s Java library installation guide shows the supported setup approach. The example below uses Java’s Duration API and Selenium’s WebDriverWait; adapt it to your installed Selenium version, browser driver, and application markup.
#1 Best Overall
Wait for and click a custom suggestion
For a custom autocomplete, type into the real input, wait for the desired suggestion to be visible, then click it. Replace the example locators and text with values from the page under test.
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AutocompleteExample {
public static void selectCity(WebDriver driver) {
WebElement input = driver.findElement(By.id("city-input"));
input.sendKeys("San");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
By desiredSuggestion = By.xpath(
"//*[contains(@class, 'suggestion') and normalize-space()='San Diego']"
);
WebElement suggestion = wait.until(
ExpectedConditions.visibilityOfElementLocated(desiredSuggestion)
);
suggestion.click();
wait.until(ExpectedConditions.attributeToBe(input, "value", "San Diego"));
}
}
The five-second timeout and the locators are illustrative, not universal Selenium requirements. A page may use a different input attribute, result container, accessible label, or selection state. Prefer a locator that identifies the intended result in the currently displayed list, rather than a broad locator that might match a hidden template or an old result.
Selenium’s explicit waits poll for a specified condition before proceeding; the Waiting Strategies guide explains dynamic page timing and Java waits. The Expected Conditions reference includes conditions such as visibility and text. Choose the condition that represents readiness for this particular widget.
Rank #2
Use keyboard selection when the widget supports it
Some autocomplete controls are designed for keyboard navigation. In that case, send the expected arrow key or keys and confirm the choice with Enter, then assert the accepted value or selected state. The number of arrow presses and the correct confirmation key depend on the widget; do not assume the first visible suggestion is the right one.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsimport java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
WebElement input = driver.findElement(By.id("city-input"));
input.sendKeys("San");
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(".suggestion-list")
));
input.sendKeys(Keys.ARROW_DOWN);
input.sendKeys(Keys.ENTER);
wait.until(ExpectedConditions.attributeToBe(input, "value", "San Diego"));
Adjust the list locator and expected value to match the application. If the widget’s focus or keyboard behavior is not clear, inspect its accessibility attributes and test the same keyboard sequence a user would use. Selenium also provides lower-level keyboard and pointer interaction through its Actions API; the element interaction guide describes sending keys and interacting with elements.
Handle a native HTML select separately
If inspection confirms the control is a native <select>, use Selenium’s Select support rather than locating a custom overlay:
Rank #3
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
WebElement countryElement = driver.findElement(By.id("country"));
Select country = new Select(countryElement);
country.selectByVisibleText("Canada");
This example applies only when the located element is a native select with options. A custom JavaScript dropdown that merely looks like a select needs the interaction supported by its actual markup.
Make synchronization match the result you need
Navigation returning does not guarantee that JavaScript-driven suggestions have been inserted or updated. Synchronize on the state that matters rather than assuming page load means the autocomplete is ready.
- Wait for the suggestion list or the specific intended suggestion to become visible.
- If the results update as characters are typed, wait for the expected text or a widget-specific state after the final input.
- After selection, verify the input value, selected option, or application state that signifies the choice was accepted.
- Avoid combining implicit and explicit waits. Selenium warns that mixing them can produce unpredictable wait times.
- Avoid arbitrary fixed sleeps as a substitute for a condition: a delay may be too short on a slow response or needlessly long when results arrive quickly.
A custom wait predicate can be useful when the widget exposes a readiness state not covered by a standard condition. Selenium’s waiting guidance describes explicit waits as polling until a specified condition is true. Keep the predicate tied to observable application state, rather than merely adding time.
Rank #4
Troubleshoot common autocomplete failures
The suggestion is not found immediately after typing
findElement may run before the application has inserted the asynchronous results. Use an explicit wait for visibility, expected text, or another state that indicates the desired result is ready.
The locator finds the wrong or hidden row
A locator may match a hidden template, an outdated result, or several suggestions. Narrow it to the intended text within the currently displayed list and wait for that result’s visible state. Selenium’s expected conditions include visibility and text checks; use the one that matches the page.
The click is intercepted or the suggestion is not interactable
Check that the dropdown is open, the desired row is visible, and no overlay obstructs it. Selenium checks whether elements can be interacted with and may raise an interaction error when they cannot. Waiting for visibility can help with timing, but it does not fix an obstructed element or an incorrect locator.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Best Value
The test uses Select on a custom dropdown
Confirm the element’s tag and options in the DOM. If the page uses an input plus a custom overlay rather than native <select> and <option> elements, use ordinary element or keyboard interactions instead.
The test passes the suggestion but the application does not accept it
Visibility only proves that a result appeared. Perform the supported click or keyboard confirmation, then wait for and assert the accepted value or application-specific state.
Or skip the browser setup
If the task is to capture a website rather than test its autocomplete behavior, ScreenshotNeo returns a screenshot or PDF from one GET request. It is a screenshot API and MCP server, not a replacement for Selenium interaction tests. Its capture process accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo API documentation for request options. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots. Sign up for free and get 1,000 screenshots a month with no card.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




