Print

Selenium IDE Locating Elements

In any automated UI tests, identifying and locating web elements is very important.

On Selenium IDE, click on the "Options..." command under Options menu and then in the Selenium IDE Options window click on the "Locator Builders" tab to find out how Selenium can identify web elements.

 

Element Locating Methods Examples
Locating by Id

This type of locator is more limited than the identifier locator type, but also more explicit. Use this when you know an element’s id attribute.

<input type="password" class="required password" id="login_login_password" name="login_password" tabindex="2" value="">

id=login_login_password

Locating by Name

The name locator type will locate the first element with a matching name attribute. If multiple elements have the same value for a name attribute, then you can use filters to further refine your location strategy. The default filter type is value (matching the value attribute).

<input type="password" class="required password" id="login_login_password" name="login_password" tabindex="2" value="">

name=login_password

Locating by XPath

 

XPath is the language used for locating nodes in an XML document. XPath extends beyond the simple methods of locating by id or name attributes, and opens up all sorts of new possibilities such as locating the third checkbox on the page.

 

One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms, or relative to an element that does have an id or name attribute. XPath locators can also be used to specify elements via attributes other than id and name.

 

Absolute XPaths contain the location of all elements from the root (html) and as a result are likely to fail with only the slightest adjustment to the application. By finding a nearby element with an id or name attribute (ideally a parent element) you can locate your target element based on the relationship. This is much less likely to change and can make your tests more robust.

Since only XPathlocators start with “//”, it is not necessary to include the xpath= label when specifying an XPath locator.

<input type="password" class="required password" id="login_login_password" name="login_password" tabindex="2" value="">

xpath=//*[@id="login_login_password"]

 

Locating by Hyperlinks by Link Text

This is a simple method of locating a hyperlink in your web page by using the text of the link. If two links with the same text are present, then the first match will be used.

<a href="http://seleniummaster.com/seleniummastertestapp/account/">Settings<img src="http://seleniummaster.com/seleniummastertestapp/theme/raw/static/images/settings.png" alt="Settings"></a>

link=Settings

Locating by DOM

 

The Document Object Model represents an HTML document and can be accessed using JavaScript. This location strategy takes JavaScript that evaluates to an element on the page, which can be simply the element’s location using the hierarchical dotted notation.

 

Since only dom locators start with “document”, it is not necessary to include the dom= label when specifying a DOM locator.

<input type="password" class="required password" id="login_login_password" name="login_password" tabindex="2" value="">

dom=document.getElementById('login_login_password')

Locating by CSS

CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML documents. CSS uses Selectors for binding style properties to elements in the document. These Selectors can be used by Selenium as another locating strategy.

<img src="http://seleniummaster.com/seleniummastertestapp/theme/default/static/images/site-logo.png" alt="Selenium Master">

css=img[alt="Selenium Master"]