What are Locators?

Introduction

Before Selenium can interact with any web element, it must first identify and locate that element on the web page. This is done using Locators.

A Locator is a strategy that tells Selenium how to find a specific HTML element such as a textbox, button, link, checkbox, radio button, dropdown, or image. Without locators, Selenium cannot perform actions like clicking buttons, entering text, selecting checkboxes, or reading information from a webpage.

Selenium provides several built-in locator strategies, including ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath. Choosing the right locator makes your automation scripts more stable, readable, and maintainable.

In this tutorial, you will learn what locators are, why they are important, the different locator strategies available in Selenium, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.


What are Locators?

A Locator is a technique used by Selenium to identify a web element on a webpage.

Every interactive element on a webpage has HTML attributes such as:

  • id

  • name

  • class

  • tag

  • href

  • type

Selenium uses these attributes to uniquely identify elements before performing actions on them.


Why are Locators Important?

Locators help Selenium to:

  • Find text boxes.

  • Locate buttons.

  • Identify links.

  • Select checkboxes.

  • Choose radio buttons.

  • Work with dropdown menus.

  • Retrieve text from webpages.

  • Perform user interactions.

Without locators, Selenium cannot interact with any element.


Common Selenium Locator Types

LocatorDescription
IDFinds an element using its unique ID attribute.
NameFinds an element using the name attribute.
Class NameFinds an element using its class attribute.
Tag NameFinds elements by HTML tag.
Link TextFinds hyperlinks using their exact visible text.
Partial Link TextFinds hyperlinks using partial visible text.
CSS SelectorFinds elements using CSS selector syntax.
XPathFinds elements using XML Path expressions.

Selenium Locator Syntax

driver.find_element(By.ID, "username")

driver.find_element(By.NAME, "password")

Practical Example

The following example demonstrates how Selenium locates web elements using different locator strategies.

The automation script opens the Selenium practice login page, locates the Username field using its ID, locates the Password field using its Name, and verifies that both elements are displayed on the page.

from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 10. Understanding Selenium Locators - What are Locators?
# Practice site: https://the-internet.herokuapp.com/login
#
# Locators are strategies used to find elements on a web page. Selenium maps
# each locator type to a find_element() call.


def test_locators_find_elements():
    driver = webdriver.Chrome()

    try:
        driver.get("https://the-internet.herokuapp.com/login")

        username = driver.find_element(By.ID, "username")
        password = driver.find_element(By.NAME, "password")

        assert username.is_displayed()
        assert password.is_displayed()

    finally:
        driver.quit()

Output

Chrome launched successfully.

Login page opened successfully.

Username textbox located using ID.

Password textbox located using Name.

Both elements are visible.

Assertion Passed.

Test Executed Successfully.

Understanding the Code

Import Required Modules

from selenium import webdriver
from selenium.webdriver.common.by import By

Imports Selenium WebDriver and the By locator class.


Launch Chrome

driver = webdriver.Chrome()

Starts a new Chrome browser session.


Open the Login Page

driver.get("https://the-internet.herokuapp.com/login")

Navigates to the Selenium practice login page.


Locate Username Using ID

username = driver.find_element(By.ID, "username")

Finds the username textbox using its unique ID attribute.


Locate Password Using Name

password = driver.find_element(By.NAME, "password")

Finds the password textbox using its Name attribute.


Verify the Elements

assert username.is_displayed()

assert password.is_displayed()

Confirms that both elements are present and visible on the webpage.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Browser Execution Flow

Launch Chrome
      │
      ▼
Open Login Page
      │
      ▼
Locate Username Using ID
      │
      ▼
Locate Password Using Name
      │
      ▼
Verify Both Elements Are Displayed
      │
      ▼
Assertions Pass
      │
      ▼
Close Browser

How Selenium Finds an Element

Selenium Command
        │
        ▼
Choose Locator Strategy
        │
        ▼
Search HTML DOM
        │
        ▼
Element Found
        │
        ▼
Return WebElement
        │
        ▼
Perform Action

Automation Testing Example

Suppose you are automating the login page of an HR Management System.

Selenium needs to:

  • Locate the username textbox.

  • Locate the password textbox.

  • Locate the Login button.

Only after locating these elements can Selenium enter credentials and perform the login operation.


Real-World Example

Consider an online shopping website.

To search for a product, Selenium must:

  • Locate the search textbox.

  • Enter the product name.

  • Locate the Search button.

  • Click the button.

  • Locate the search results.

Every automation step depends on accurately locating web elements.


Common Mistakes Beginners Make

Using the Wrong Locator

If the locator value does not match the webpage, Selenium throws a NoSuchElementException.

Always inspect the webpage carefully before writing locators.


Using Dynamic Attributes

Some websites generate IDs dynamically.

Avoid using locators like:

id="user_12345"

These values may change every time the page loads.


Choosing Complex XPath When ID Exists

Less Preferred

driver.find_element(By.XPATH, "//input[@id='username']")

Better

driver.find_element(By.ID, "username")

Use the simplest and most stable locator whenever possible.


Best Practices

  • Prefer ID whenever available.

  • Use Name if ID is unavailable.

  • Use CSS Selector for faster and cleaner locators.

  • Use XPath only when other locators are not suitable.

  • Avoid dynamic IDs and unstable attributes.

  • Keep locators simple and readable.

  • Verify that the element is displayed before interacting with it.


Conclusion

Locators are the foundation of Selenium automation because they enable Selenium to identify and interact with web elements. Choosing the appropriate locator strategy improves test reliability, readability, and maintainability. Understanding locators thoroughly is essential before learning advanced Selenium topics such as XPath, CSS Selectors, waits, and page object models.


Frequently Asked Questions (FAQs)

What are locators in Selenium?

Locators are strategies used to identify and locate web elements on a webpage.


Which class contains Selenium locator strategies?

from selenium.webdriver.common.by import By

Which locator is the fastest?

ID is generally the fastest and most reliable locator because it is intended to be unique within a webpage.


What happens if Selenium cannot find an element?

Selenium throws a NoSuchElementException.


Which locator should I prefer?

The recommended order is:

  1. ID

  2. Name

  3. CSS Selector

  4. XPath (when other locators are not suitable)


Key Takeaways

  • Locators are used to identify web elements in Selenium.

  • Selenium cannot interact with elements without locators.

  • Common locator strategies include ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath.

  • find_element() is used to locate a single web element.

  • Prefer simple and stable locators such as ID and Name whenever possible.

  • Avoid using dynamic attributes that change frequently.

  • Understanding locators is fundamental to writing reliable Selenium automation scripts.

  • Locators are one of the most important Selenium interview topics and form the foundation of all Selenium automation.