selenium locators

What Is Locators in Selenium: All You Need To Know

Locators are the way that identifies the HML elements on the webpages, and approximately all the UI automation tools offer the capacity to use locators for identifying HTML elements on the webpages. Selenium follows the same trend and it also possesses the ability to use “Locators” for identifying HTML elements, which are known as “Selenium Locators”. Selenium offers support to different types of locators.

What are the locators in Selenium?

Identifying the correct GUI element on the webpage works as a pre-requisite for creating successful automation scripts. This is the place where locators come into the picture. Locators work as one of the most vital components of Selenium infrastructure that help Selenium scripts in identifying the WebElements uniquely that are present on the webpage. You can learn about these locators by joining Selenium online training.

What locators does Selenium support?

To access the locators, Selenium offers “By” class, which assists in locating the elements within DOM. It offers various methods like CssSelector, className, name, LinkText, id, xPath, etc. that can identify the web elements depending on the corresponding locator strategies. So, one can identify all the supported locators by Selenium quickly by browsing all the visible methods on the “By” class. Here come the details:

Locating elements by Name: In HTML 5, often form elements come with a name attribute associated with those. Here the .find_element_by_name() method just returns the initial element with the matching class. In case there are several elements of a similar name, then only the initial corresponding element would return. No matching elements lead to the NoSuchElementException error.

Locating elements by CSS ID: This is probably the simplest method to locate an element. The CSS ID that is stored in the ID attribute of the HTML DOM element is unique for every element available in page by design. Therefore, an ID can identify an element uniquely. One has to call the .find_element_by_id() method of the webdriver class to use this feature.

Locating elements by CSS Class: It is another important strategy of locating elements on a webpage. Generally, the class element is piled up in the class attribute of the HTML tag. By design, the CSS class is applied to the group of DOM elements. The method .find_element_by_class_name() only returns the very first element with the similar class. And it raises the NoSuchElementException error in case no element is found with the mentioned class name.

Locating elements by XPath: If one fails to identify the element by name, class, or ID, then the element can be located through the XML path. This method can also be implemented while reading the XML documents. There are two types of XPath locators:

  • Absolute Xpath: It generates the path from the root node of the document and generally begins with a single slash (/). The root node begins from tag HTML.
  • Relative Xpath: This can generate a path in the document at any positive and use double slash (//) to begin.

Locating elements by LinkText: Finding an element on a webpage with LinkText is quite simple. But the only criterion is that there should be only one unique link on that webpage. In case there are several links with the similar link text, Selenium would act on the first identical element with the link.

Locating elements by Partial LinkText: Partial LinkText also works in a similar pattern like LinkText. This locator helps to identify partial text to links.

Locating elements by Class Name: There can be many elements with a similar name. So, if you use findElementByClassName to find out that element on the webpage, then make sure it is just one. If not, then you have to extend it by using the class name and the sub-elements.

Locating elements by TagName: You can use TagName with different group elements on the webpage like check-boxes/dropdowns and select.

Best practices of Selenium Locators:

Learning about the concept of Selenium locators is one thing, but learning how to use those locators is a completely different matter. The ability to develop a robust locator starts with a thorough understanding of what a robust locator is. So, here are three important criteria that one must follow while using these Selenium locators:

  1. The locators in Selenium are as easy and small as it can be: The more elements a locator has, the higher the scopes that the Selenium locator will break due to the change in the structure of the webpage.
  2. Selenium locators will continue to work even after the properties of a UI element are changed: Depending on the frequently-changed factors like modifier classes is not a good practice ever.
  3. The robust Selenium locators will continue to work even after changing the UI elements around all the elements that you target: Every time you will use a non-unique attribute, there is a scope that the Selenium locators will break as someone will add an element with the similar attribute above.

About the Author

Leave a Reply

Your email address will not be published. Required fields are marked *