Handle Autocomplete in Playwright Java

Profile picture for user arilio666

This article will show how we can handle autocomplete using playwright java.

We are going to use this site called https://demo.automationtesting.in/AutoComplete.html.

    String expectedText = "Guatemala";

        Locator autoC = page
                .locator("//div[@class='ui-autocomplete-multiselect ui-state-default ui-widget ui-state-active']");
        int autoCcount = autoC.count();


        for (int i = 0; i < autoCcount; i++) {

            String autoCText = autoC.nth(i).textContent();
            if (autoCText == expectedText) {



  • First, we are going to navigate to the respective page.
  • Then we take the locator of the search box where autocomplete suggestions appear.
  • We will get the count of the locator and then use this count inside for loop.
  • Using the nth and passing in the iterated value, we get the text content of the autocomplete suggestion box.
  • Then using the locator we tool earlier, we click on the expected string we stored using the if statement.