Let's get started with a Microservice Architecture with Spring Cloud:
Select Text From the Autocomplete Input using Selenium
Last updated: January 7, 2026
1. Overview
Modern web applications often contain a search bar that returns dynamic suggestions based on user input. Writing end-to-end (E2E) tests requires more than inputting text in a search bar. It also involves waiting for the suggestion dropdown to pop up and selecting an item from the dropdown.
In this tutorial, we’ll explore how to use Selenium to interact with an autocomplete-enabled search bar on a website.
2. Test Scenario
Let’s set up a test scenario that could illustrate how we select an autocomplete item from the search:
- Navigate to the eBay website
- Find the search bar at the top page
- Type “iphone” into the search input field
- When the autocomplete suggestions pop up, select the second item from the list
3. Test Setup
To start with writing a test with Selenium, we have to add the selenium-java dependency to our pom.xml:
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.38.0</version>
</dependency>
Besides the core Selenium dependency, we also need the WebDriver to drive a browser. Let’s add the webdrivermanager dependency as well:
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>6.3.3</version>
</dependency>
This manager package is a popular Java library that includes various browser WebDriver such as Chrome, Edge, and Firefox.
In our test, we’ll use JUnit to manage the lifecycle of the Selenium WebDriver. A Chrome browser will be initialized and cleaned up in each test:
private WebDriver driver;
@BeforeEach
void setup() {
driver = new ChromeDriver();
}
@AfterEach
void teardown() {
driver.quit();
}
4. Test Implementation
Below is our test method to perform the test scenario. We define two constants, XPATH_INPUT and XPATH_AUTOCOMPLETE_ITEMS, to represent the XPath of the search bar element and the autocomplete suggestion dropdown items:
private static final String XPATH_INPUT = "//div[@id='gh-search-box']//input";
private static final String XPATH_AUTOCOMPLETE_ITEMS = "//ul[@id='ebay-autocomplete']/li";
@Test
void whenUserNavigatesToEBays_thenSelectThe2ndAutoCompleteItemFromSearchInput() throws Exception {
driver.navigate().to("https://www.ebay.com");
WebElement inputElement = driver.findElement(By.xpath(XPATH_INPUT));
String text = "iphone";
for (char c : text.toCharArray()) {
inputElement.sendKeys(Character.toString(c));
Thread.sleep(50);
}
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(3));
List<WebElement> autoCompleteElements = wait.until(
ExpectedConditions.visibilityOfAllElementsLocatedBy(
By.xpath(XPATH_AUTOCOMPLETE_ITEMS)
)
);
assertThat(autoCompleteElements.size()).isGreaterThanOrEqualTo(2);
WebElement secondItem = autoCompleteElements.get(1);
secondItem.click();
}
This implementation first opens a Chrome browser and navigates to the eBay website, then locates the search bar by the XPath of the corresponding HTML element.
Next, we input the keyword “iphone” character by character, mimicking the human input. Note that we could not send the whole keyword in one go on eBay:
inputElement.sendKeys(text)
Sending the entire keyword will cause the suggestion dropdown menu not to appear.
The keyword input will trigger a request to eBay to call their API to get the suggestions. It may take a bit of time for the server to return the suggestions response. Thus, we use WebDriverWait to wait for the autocomplete dropdown to appear. Once the element appears, we select the second suggestion from the dropdown.
5. Conclusion
In this article, we demonstrated how to use Selenium to write an E2E test to simulate human inputs to an autocomplete search bar and an explicit wait to select one of the suggestions returned. This approach ensures the reliable selection of suggestion items.
As usual, the complete source code in this article is available over on GitHub.















