Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium how to select an object by class

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")
like image 228
aron Avatar asked Apr 04 '11 20:04

aron


People also ask

In which package select class is present in Selenium?

The select class allows us to select an element from the drop-down and lists that are created with the HTML <select> element. The select class is present in org. openqa. selenium.

What is the use of by class in Selenium?

Initializes a new instance of the By class using the given functions to find elements. Gets or sets the value of the description for this By class instance. Gets or sets the method used to find a single element matching specified criteria. Gets or sets the method used to find all elements matching specified criteria.

Can we find element based on class name using Selenium?

We can find an element using the attribute class name with Selenium webdriver using the locators - class name, css, or xpath.


2 Answers

Use XPath selector in Selenium:

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

not checked, might require some corrections.

like image 109
Tomasz Nurkiewicz Avatar answered Nov 16 '22 08:11

Tomasz Nurkiewicz


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
like image 31
Ben Avatar answered Nov 16 '22 09:11

Ben