I am working in Selenium. I'm using Java language. I am following page object design pattern. I want to automate an autocomplete search test box. After entering a single character or multiple character autocomplete should display the name in the list. I want to automate this scenario.

Here is my code:

    WebElement category;
 public void setCategory(String cat){



AppointmentPage appointmentPageobj= PageFactory.initElements(DriverManager.driver,AppointmentPage.class);


 AppointmentPage ex=log.Valid("9845612345","naveen","[email protected]","Demo","2017-03-03","Dr. Bijoy Balakrishnan");

Screenshot: Screenshot of website with dropdown

  • You should see a list of drop down list elements, select one from this list.
    – Yu Zhang
    Commented Mar 3, 2017 at 9:42
  • @YuZhang How to select from the list ?
    – user24314
    Commented Mar 3, 2017 at 9:56
  • Does the answers at stackoverflow.com/questions/663034/… help?
    – Kate Paulk
    Commented Mar 3, 2017 at 12:42

3 Answers 3


I will write pseudocode that might pose some challenge (or not at all, depending on your experience), but is very robust when coded properly. Use explicit waits only.

I hope this way, you will learn something and also find your solution.

selectOptionWithText(string cat)
  sendKeys(cat); // put this in your method instead of before
  wait.Until(dropdown with expected value == visible);
  click expected value;
  wait.Until(dropdown != visible);
  wait.Until(expected value is in the input field);

Try this:

driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
listItems.get(0).click(); //make sure you have enough implicit wait on Global level to wait till list appear

I use following approach in C#, you can use parallel approach in Java Define a WebElement collection element with IList type for values in dropdown,as each dropdown value will have same class

[FindsBy(How = How.ClassName, Using = "<className Of the dropdown values>")]
public IList<IWebElement> DropDownOptionsList { get; set; }

public void selectOptionWithText(string cat)
            if (cat.Length >= 1)
                foreach (IWebElement item in DropDownOptionsList)

                    if (item.Text ==cat)

 public void setCategory(String cat){
        category.sendKeys(cat.Substring(0, <NumberofCharactersYOuWantToEnter>));

  • @AmoIO please help me to write the code in my above page object pattern. so I can understand
    – user24314
    Commented Mar 3, 2017 at 9:57
  • Send me the class name of dropdown elements, and minimum number of caharacters to enter in dropdown. I will share sample C# code with you, as I am not a java person. Commented Mar 3, 2017 at 10:01
  • Thread.Sleep = bad. Also this method isn't robust at all - what if the correct value takes a few seconds to load?
    – FDM
    Commented May 2, 2017 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.