What are Locators?
A locator is a command tells Selenium IDE which GUI elements it needs to operate on. Identification of correct GUI elements is a prerequisite to creating an automation script. The identification of GUI elements is more difficult sounds. Sometimes, you end up working with incorrect GUI elements or no elements at all. Hence, Selenium provides many Locators for locating a GUI element
Types of Locators in Selenium IDE
- ID
- Name
- Link Text
- CSS
- DOM (Document Object Model)
- XPath
Locating by ID
This is the most common way of locating elements. the ID's supposed to be unique for each element. Target Format: id=id of the elementLocating by Name
Locating elements by name is very locating by ID. that we use the "name=" prefix instead. Target Format: name=name of the elementLocating by Name using Filters
Filters use when many elements have the same name. Filters are extra attributes used to distinguish elements with the same name. Target Format: name=name_of_the_element filter=value_of_filterLocating by Link Text
This type of locator apply to hyperlink texts. We access the link by prefixing our target with "link=". then followed by the hyperlink text. Target Format: link=link_text .Locating by CSS Selector
CSS Selectors are string patterns used to identify an element. it based on a combination of HTML tag, id, class, and attributes. It is more complicated than the previous methods. but it is the most common locating strategy of advanced Selenium users. it can access elements have no ID or name. CSS Selectors have many formats, but we focus on the most common ones. - Id & Tag
- Tag and class
- Attribute and Tag
- Tag, class, and attribute
- Inner text when using this strategy, we always prefix the Target box with "css=".
Locating by CSS Selector - Tag & ID
Inner texts are the actual string patterns that the HTML label shows on the page.Locate CSS Selector - Tag and Class
Locating by CSS Selector using an HTML tag and a class name is like using a tag and ID, but in this case, a dot (.) is used instead of a hash sign.CSS Selector - inner text
HTML labels are seldom given id, name, or class attributes. Inner texts are the actual string patterns HTML label shows on the page.Locating by DOM
The Document Object Model in simple terms is the way by which HTML elements structured. Selenium IDE is using the DOM access page elements. If we use this method, our Target box will always start with "dom=document...". but, the "dom=" prefix removed because Selenium IDE is able to interpret anything. that starts with the keyword "document" to be a path within the DOM anyway. There are four basic ways element through DOM - getElementById
- getElementsByName
- dom:name
- dom:index
Locating by XPath
XPath is the language used when locating XML (Extensible Markup Language) nodes. Since HTML can be thought of as an implementation of XML, we can also use XPath in locating HTML elements.Advantage:
- It can access almost any element, even those without class, name, or id attributes.
Disadvantage:
- It is the most complicated method of identifying elements because of too many different rules and considerations.