Selenium is a popular open-source framework for automating web browsers, widely used for web testing and web scraping tasks. One of the fundamental concepts in Selenium automation is “locators.” Locators are elements used to identify and interact with web elements on a webpage. In this blog, we will explore the different types of locators in the Selenium framework, how to use them effectively, and when to choose one over the other.
What is Selenium?
One of the most popular open-source automation tools is Selenium. Additionally, it offers automation across numerous programming languages, platforms, and browsers. It may be quickly installed on a variety of operating systems, including Windows, Linux, Solaris, and Macintosh. Similar to that, it supports OS (Operating Systems) for mobile applications including iOS, Windows Mobile, and Android.
What are Locators?
Elements on a page can be located using a locator. It is the argument that is passed to the methods for locating items. Additionally, findElement() and findElements() are used to locate elements in Selenium WebDriver.
Types of Locators in Selenium
2. CSS Selector
6. PartialLink text
The best and quickest method for finding requested WebElements on a page is to use this locator in Selenium. For each element in the DOM, an ID Selenium locator is specific.
IDs are thought to be the quickest and safest way to discover items because they are distinct for each one on the page.
Driver.findElement(By.id (element ID));
Here are the steps to locate ID locators on a website:
- In order to find an ID locator, we must first visit the website, navigate to the relevant field, click the right mouse button, and then choose the examine option from the list.
- Once there, we select the ID selector by going to the page below.
One of the many properties that can be used to define an element is Name. The Selenium WebDriver name locator can be used to find elements similarly to the ID locator.
The Name locator may or may not have a unique value, unlike ID locators, which are exclusive to a page. The locator chooses the first WebElement with that name on the page if there are other elements with that name.
The fundamental syntax used for using XPath as a CSS locator in Selenium WebDriver is shown below: It helps in identifying elements on the web page using XML expressions.
CSS Selector Locator:
The styling of web pages is done using CSS (Cascading Style Sheets). The location of WebElements in the DOM can also be accomplished via CSS, which is another popular method.
If you can’t find an element using ID or Name locators, Selenium’s CSS Selector should be your go-to option. Over the XPath locator, it can be chosen.
Class Name Locator:
The class attribute is used to define WebElements, which are located using the class name locator.
Link Text Locator:
Link text that is present in the hyperlinks can be used to find elements. For instance, if there are numerous links with the same text, the first link will be used.
But only elements with an anchor(a) tag can use this Identifier approach.
driver.findElement(By.linkText("Click here for more info"));
Partial Link Text Locator:
Similar to Selenium’s standard Link Text locator, a facility exists to locate a web element using Partial Link Text. In cases where the link text is excessively extensive, finding WebElements using partial link text is preferred.
In this case, a partial text can be used to locate a distinctive element and use it to conduct additional operations on it. When numerous links on a website share a portion of text, this method can occasionally be used to find them all.
Tag Name Locator:
The elements matching the specified Tag Name are located using the tag name finder. When we want to extract the content from a Tag, it is really beneficial.
List<WebElement> inputElements = driver.findElements(By.tagName("input"));
Selenium provides a rich set of locators to interact with web elements, each with its strengths and use cases. Your choice of locator should depend on factors like element uniqueness, performance, readability, stability, and compatibility. By mastering the art of choosing the right locator, you can build robust and maintainable automated test scripts for your web applications.
THANKS FOR YOUR PRECIOUS TIME
EPEDAGOGUE GLOBAL PVT LTD
PRAKASH CHAND THAPLIYAL