20

I have a web page with a form and has a field that uses the jquery autocomplete function.

enter image description here

This is how the HTML renders after a user name returns 1 or more results.

However I cannot figure out how to make Selenium "click" a result.

enter image description here

Can I do a jQuery type of selector. e.g.

$(".ul.ui-autocomplete li:first a")
aron
  • 2,856
  • 11
  • 49
  • 79

4 Answers4

27

Use XPath selector in Selenium:

xpath=//li[contains(@class, 'ui-autocomplete')]/li[1]/a

not checked, might require some corrections.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Hey Tomasz do I put this directly into the "Target" field? Do I need to do anything else? – aron Apr 05 '11 at 14:56
  • 4
    Yes, it should work. Please take a look at *Element Locators* chapter in: http://release.seleniumhq.org/selenium-core/1.0.1/reference.html – Tomasz Nurkiewicz Apr 05 '11 at 15:14
  • 1
    The Selenium reference page @Tomasz indicated is a good starting point, but for a comprehensive cookbook of many commonly used XPath, CSS, and DOM recipes for use with Selenium, see my just-published [Rosetta Stone and Cookbook](http://www.simple-talk.com/dotnet/.net-framework/xpath,-css,-dom-and-selenium-the-rosetta-stone/) on Simple-talk.com. – Michael Sorens Apr 05 '11 at 18:43
3

in response to "Can I do a jQuery type of selector," jQuery uses CSS selectors. Selenium can also use CSS selectors; just prefix the selector with "css=". so:

css=.ul.ui-autocomplete li:first a
Ben
  • 719
  • 6
  • 25
0

Next way to use xpath like this

xpath=/html/body/ul[2]/li[1]/a
cyberoot
  • 340
  • 2
  • 18
0

Suppose you have a dynamic XPATH then you can point to an element like this

driver.findElement(By.className(""));