Can selenium handle autocomplete?
I used following sequence in IDE,
- typeKeys
- waitForTextPresent
- mouseOver
- clickAt
and worked well
For WebDriver, try this
The below code is for searching a text automatically from the auto suggest; mainly for a list item.
driver.findElement(By.id("your searchBox")).sendKeys("your partial keyword");
Thread.sleep(3000);
List <WebElement> listItems = driver.findElements(By.xpath("your list item locator"));
listItems.get(0).click();
driver.findElement(By.id("your searchButton")).click();
I'd do this as follows:
type
to enter the value in the text field.waitForTextPresent
orverifyTextPresent
to check the autocomplete contentclick
ormouseDown
to click on the item in the autocomplete list
The trick is going to be making the final click
be just in the right place. You should be able to use an XPath expression that searches for the text you're expecting to find it.
The type command may not be enough to trigger the autocomplete. Dave Webb's suggestions are otherwise spot on. My only addition would be that you might need the typeKeys command, which causes slightly different JavaScript events to be fired, which may be more likely to trigger the autocomplete widget.