• Articles

Locators in Selenium - A Comprehensive Guide

Locators in Selenium - A Comprehensive Guide

Table of content

Show More

Selenium provides various methods to locate and interact with web elements on a webpage. These methods are known as Locators. In this blog, we will discuss Locators in Selenium, their types, and their importance in testing.

Watch the video below to learn Selenium from scratch

Video Thumbnail

What are Locators?

Locators in Selenium are methods used to identify and locate web elements on a web page. They are essential to automation testing as they help automate discovery and interaction with web elements. 

What are Locators Used For?

Locators are used to identify and navigate web elements on a webpage. Once an element is found, we can perform various actions like clicking, typing, selecting, etc. Locators also verify an element’s presence, absence, or visibility on a webpage.

Locators in Selenium are a powerful tool for automation testing and are an essential part of any testing strategy. Understanding Locators in Selenium is crucial for automation testing, as it can help save time and effort in the testing process. Using Locators, we can automate repetitive and time-consuming tasks, and also, ensure our web application functions correctly. 

What are Locators in Testing?

In testing, Locators are used to automate the testing process. Automation testing is using tools to automate repetitive and time-consuming tasks. Locators in Selenium help automate the process of locating and interacting with web elements on a webpage.

Different Types of Locators in Selenium

Different Types of Locators in Selenium

Selenium provides eight types of locators, but what are the names of those 8 locators?

The locators are as follows:  ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath.  Now, let’s examine each of them in detail.

ID Locator

  • Used to find an element using its unique ID.
  • The fastest and most reliable way to locate an element.
  • Ideal when the ID of an element is unique on a webpage.

Name Locator

  • Used to find an element using its name attribute.
  • Name attribute may not be unique, but it is commonly used to identify elements.

Class Name Locator

  • Used to find an element using its class name.
  • Multiple elements can have the same class name, but it is still a reliable way to locate elements.

Tag Name Locator

  • Used to find an element using its HTML tag name.
  • Useful when only one element has a particular tag name.

Get 100% Hike!

Master Most in Demand Skills Now!

  • Used to find an anchor element (a tag) with a specific link text.
  • Used to identify links on a webpage.
  • Used to find an anchor element (a tag) with partial link text.
  • Used to identify links with a familiar pattern.

CSS Selector Locator

  • Used to identify elements using CSS selectors.
  • CSS selectors are used to define the style of elements on a webpage.

XPath Locator

  • Used to identify elements using XPath expressions.
  • XPath is a language used to navigate through XML documents.

Locators in Selenium Webdriver

Selenium Webdriver is a tool that automates web applications. It provides various methods to locate and interact with web elements on a webpage. These methods are based on the types of locators we discussed above. Some of the examples of these locators are:

  • ID: Matches by element ID. For example <button id=”submit”>. 
    Usage: driver.find_element(By.ID, “submit”)
  • Class Name: Matches by element class name. For example <button class=”btn”>.
    Usage: driver.find_element(By.CLASS_NAME, “btn”)
  • Name: Matches by element name. For example <button name=”button1″>. 
    Usage: driver.find_element(By.NAME, “button1”)
  • Link Text: Matches anchor element by exact text. For example <a href=”https://example.com”>Click here</a>. 
    Usage: driver.find_element(By.LINK_TEXT, “Click here”)
  • Partial Link Text: Matches anchor element by partial text. For example <a href=”https://example.com”>Click here to register</a>.
    Usage: driver.find_element(By.PARTIAL_LINK_TEXT, “here to”)
  • Tag Name: Matches by HTML tag name. For example <button> 
    Usage: driver.find_element(By.TAG_NAME, “button”)
  • CSS Selector: Matches by CSS selector. For example <button class=”btn primary”> 
    Usage: driver.find_element(By.CSS_SELECTOR, “.btn.primary”)
  • XPath: Matches by XPath query. For example //button[@class=’btn primary’]. 
    Usage: driver.find_element(By.XPATH, “//button[@class=’btn primary’]”)

Advantages of  Locators in Selenium WebDriver

Advantages of  Locators in Selenium WebDriver

There are several advantages of using locators in Selenium WebDriver:

Locators are stable

Reliable locators like ID, class name, and CSS selectors will continue to locate the same elements, even if the page HTML changes slightly. This makes tests less brittle.

Locators improve reusability

Once we define a locator for an element, we can reuse that locator every time we want to find that element. This avoids duplication in test scripts.

We can use multiple locators for the same element

This adds redundancy and makes tests more robust. If one locator fails for some reason, the test can continue using another locator for the same element. 

Locators abstract away the underlying HTML

Tests without having full knowledge of the page HTML structure and markup. They simply use locators to find elements without worrying about the HTML intricacies. This makes tests less prone to breaking due to minor HTML changes.

Different locators suit different needs

ID and class name locators are fast and stable but require well-defined element attributes. XPath is flexible but can be slow. CSS is in the middle. We can choose the appropriate locator based on the element we want to find.

Locators work across all browsers and platforms

Since locators interact with the page Document Object Model  (DOM) , they function consistently regardless of the underlying browser, operating system, or other variables.

Conclusion

Mastering locators is key to building efficient and robust test automation frameworks with Selenium. With a firm grasp of locators, you will be able to automate interactions with buttons, links, text boxes, drop-downs, and any other elements on a web page. 

While the page content and UI may change over time, locators continue to provide a steady way for tests to navigate the web application confidently. They serve as an essential abstraction layer between the volatile world of web pages, and the structured world of automated checks.

Overall, spend some time learning and practicing with the various locators available in the Selenium library. A strong foundation in locators will make you a more productive and capable user of Selenium WebDriver. With locators by your side, you can build automated tests for any web application, and ensure it remains bug-free.

Course Schedule

Name Date Details
Testing Courses 14 Dec 2024(Sat-Sun) Weekend Batch View Details
21 Dec 2024(Sat-Sun) Weekend Batch
28 Dec 2024(Sat-Sun) Weekend Batch