Handle autocomplete using selenium

Last updated on by michaelyin

Selenium is a so great tool to make web ui test, in some cases, we have to make it work with some ajax autocomplete like jquery ui autocomplete.

First you need to understand how the autocomplete work. Most plugin will create a ul which contain data get from the server by ajax, and insert the ul into the body, then display it for user to click by mouse or enter by keydown.

When the page load, you can see the ul has no children

Then I type in some word in the input to make the plugin make ajax query

As you can see, plugin use the data returned from server create li in ul element and display it, so how to make Selenium wait for ajax complete? Selenium provide WebDriverWait which can wait for something done.

The next thing to do is to choose the item, user can click or enter to choose the item, so there are at least two ways to make selenium work with autocomplete

#first set value to input to make ajax start
self.wd = CustomWebDriver()

#selenium will wait until it find li in ul, outtime is 5
WebDriverWait(self.wd , 5).until(
    lambda driver :

#after ajax complete, simulate click operation

The code above need to be modified when you use it because some detail between plugin is not same

You can also choose enter to choose the item, all roads lead to rome.

Send Me Message

Tell me more about your project and see if I can help you.

Contact Me