In this article we learn the what are the 8 locators in selenium. Locators in selenium are the way to identify an HTML element on a web page, and almost all UI automation tools provide the capability to use locators in selenium for the identification of HTML elements on a web page.
locators in Selenium
Locators in selenium are one of the essential components of Selenium infrastructure, which help Selenium scripts in uniquely identifying the WebElements (such as text box, button, etc.) present of the web page.
DOM can be accessed in Google Chrome either by pressing F12 or by right click on the web page and then by selecting Inspect.
Once we click on the “Inspect option”, it will open the Developer Tools console. By default, it will open the “Elements” tab, which represents the complete DOM structure of the web page. Now, if we hover the mouse pointer over the HTML tags in the DOM, it will highlight the corresponding elements it represents on the webpage.
What are the 8 locators in Selenium?
1) ClassName – A ClassName operator uses a class attribute to identify an object.
2) cssSelector – CSS is used to create style rules for webpages and can be used to identify any web element.
3) Id – Similar to class, we can also identify elements by using the ‘id’ attribute.
4) linkText – Text used in hyperlinks can also locate element
5) name – Name attribute can also identify an element
6) partialLinkText – Part of the text in the link can also identify an element
7) tagName – We can also use a tag to locate elements
8) xpath – Xpath is the language used to query the XML document. The same can uniquely identify the web element on any page.
1) Do not use dynamic attribute values to locate elements, as they may change frequently and result in breakage of locator script. It also severely affects the maintainability, reliability, and efficiency of the automation script.
2) ID and name attributes take precedence over other locators if web page contains unique ID and name, then it’s always advisable to use them instead of XPath as they are faster and more efficient.
3) While using locators in selenium, make sure that locator points precisely to the required element. If the needed scenario needs to perform some operation on a single element, then make sure that locator exactly matches to only one element. If the locator points to several different elements, then it may cause breakage in our script.