What are Locators?
Selenium uses what is called locators to find and match the elements of the webpage that it needs to interact with. Locators are the lifeblood of the tests. Using the right locator ensures the tests are faster, more reliable or has lower maintenance over releases. If you’re fortunate enough to be working with unique IDs and Classes, then you’re usually all set. But there will be times when choosing the right locator will become a nightmare. It can be a real challenge to verify that you have the right locators to accomplish what you want.
Locators in selenium IDE
For many Selenium commands, a target is required. This target identifies an element in the content of the web application, and consists of the location strategy followed by the location in the format locatorType=location. The locator type can be omitted in many cases.
The various locator types are explained below with examples for each.
Locator: ID
On web applications today, elements will have an ID attribute for all their buttons. This allows Selenium to find the unique item, as IDs should be unique, and then complete the action that it needs to do against that element.
Id=log
Locator: Name
Elements do not necessarily have name attributes on all of them. Elements can have names that we can use to locate them. In the Target textbox, this would look like name=Element.
name=log
Locator: Link
The most common element on a page is a link. Links allow pages to be joined together so that end users can navigate your site with ease.
link=Register
Locator: CSS
The CSS locator finds the first element with a specific CSS class attribute. This is useful for locating items that have a unique style on the page.
Example: If an element is given like this
Locator:Xpath
XPath is a language for traversing the structure of the DOM (document object model) of the web page. XPath locators are very powerful and flexible. Any element on the page can be located via one or more XPaths and most other locators can be expressed as an XPath. Excepting CSS Selectors, no other locators share this feature.
There are also a couple of very useful Firefox Add-ons that can assist in discovering the XPath of an element:
- Firebug
- XPath Checker
- Firepath
- XPath Finder
Locator: 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.
dom= dom=document.getElementById(‘id’) for this image its dom= dom=document.getElementById(‘log’)