Skip to main content
Blogs

Top Locators In Selenium and How To Use Them

By May 3, 2023August 17th, 2023No Comments3 min read
Top Locators In Selenium and How To Use Them

Selenium is a highly regarded open-source web automation tool used for testing web applications. It supports multiple programming languages such as Java, Python, C#, etc., and is widely acclaimed for automation testing. Selenium provides various locators to identify web elements on a web page which are essential for automation testing, as they help to interact with web elements to perform operations.

In this blog, we shall delve into the types of locators in selenium and provide insights on their efficient usage.

What Are The 8 Locators In Selenium?

1. ID Locator:

ID Locator is the most common and popular locator used in Selenium. It is used to identify web elements based on the ‘id’ attribute of the HTML tag. It is the fastest locator and is recommended by Selenium to use whenever possible. In order to utilize the ID locator, you must specify the ‘id’ value of the web element in the code

Example: WebElement element = driver.findElement(By.id(“element_id”));

Name Locator:

The Name Locator is used to identify web elements based on the ‘name’ attribute of the HTML tag. It is slower than the ID Locator but is still widely used. To employ the Name Locator, the ‘name’ value of the web element must be provided in the code.

Example: WebElement element = driver.findElement(By.name(“element_name”));

3. Class Name Locator:

Class Name Locator is used to identify web elements based on the ‘class’ attribute of the HTML tag. It is used when the web element has a unique class name. It is faster than the Name Locator but slower than the ID Locator.

Example: WebElement element = driver.findElement(By.className(“element_className”));

4. Xpath Locator:

XPath is a powerful and flexible locator used to identify web elements based on their attributes and hierarchy in the HTML page. It is slower than the other locators, but it can identify any element on the web page. XPath can be used with attributes such as ‘id’, ‘class’, ‘name’, etc. It is recommended to use relative XPath(starts with //) rather than absolute XPath(starts with /). We can also use XPath axes to identify an element such as ancestor, descendant, following, following-sibling, preceding and many more.

Example: WebElement element = driver.findElement(By.xpath(“//input[@id=’element_id’]”));

5. CSS Selector Locator:

CSS Selector Locator is used to identify web elements based on their CSS attributes such as ‘class’, ‘id’, ‘name’, etc. It is faster than XPath Locator but slower than ID Locator.

Example: WebElement element = driver.findElement(By.cssSelector(“input[id=’element_id’]”));

6. LinkText Locator: 

LinkText Locator is used to identify web elements based on their exact hyperlink text. These can only be used for elements containing anchor(<a>) tags.

Example: WebElement element = driver.findElement(By.linkText(“Testrig Technologies”));

7. PartialLinkText Locator: 

Similar to LinkText, PartialLinkTest Locator is used to identify web elements based on their part of the hyperlink text.

Example: WebElement element = driver.findElement(By.partialLinkText(“Testrig”));

8. TagName Locator: 

TagName Locator is used to identify web elements based on their tag name which is HTML tag such as ‘input’, ‘div’, ‘button’, etc. It returns all the elements from the page that contains a specified tag. This method simplifies the process of handling tables.

Example: WebElement element = driver.findElement(By.tagName(“input”));

Conclusion

In conclusion, there are several locators available in Selenium to identify web elements, each with its own advantages and disadvantages. ID Locator is the fastest and recommended by Selenium, while XPath can identify any element on the web page. It is essential to choose the right locator for automation testing to ensure the accuracy and speed of the test. Selenium provides flexibility to use any locator depending on the requirement, making it a versatile tool for web automation testing.

Are you looking to outsource Automation Testing? Experience Seamless Selenium Testing Support with Testrig Technologies – The Industry Leader in Automation Testing. As the Top Selenium Testing Company, We Offer a Wide Range of Services to Meet Your Testing Needs. Contact Us Today to Learn More!

Leave a Reply