Locator Strategy

Introduction

A Locator Strategy is the method Selenium uses to identify and locate web elements on a webpage. Every HTML element contains attributes such as id, name, class, tag, href, or text, and Selenium uses these attributes through different locator strategies to interact with elements.

Choosing the correct locator strategy is one of the most important skills in Selenium automation. A well-chosen locator makes your automation scripts faster, more reliable, and easier to maintain.

Selenium supports eight built-in locator strategies:

  • ID

  • Name

  • Class Name

  • Tag Name

  • Link Text

  • Partial Link Text

  • CSS Selector

  • XPath

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


What is a Locator Strategy?

A Locator Strategy is the technique Selenium uses to search for and identify an element in the HTML Document Object Model (DOM).

When Selenium executes a command such as:

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

it searches the webpage for an element whose id attribute is username.

Once the element is found, Selenium returns it as a WebElement, allowing further interactions such as typing text or clicking buttons.


Why are Locator Strategies Important?

Locator strategies allow Selenium to:

  • Locate textboxes.

  • Find buttons.

  • Identify links.

  • Locate checkboxes.

  • Select radio buttons.

  • Work with dropdowns.

  • Read webpage content.

  • Perform user interactions.

Without locator strategies, Selenium cannot identify elements on a webpage.


Selenium Locator Strategies

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

Syntax

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

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

driver.find_element(By.CLASS_NAME, "button")

driver.find_element(By.TAG_NAME, "input")

driver.find_element(By.LINK_TEXT, "Login")

driver.find_element(By.PARTIAL_LINK_TEXT, "Log")

driver.find_element(By.CSS_SELECTOR, "#username")

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

Practical Example

The following example demonstrates multiple Selenium locator strategies.

The automation script opens the Selenium practice website and locates elements using ID, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath.

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


# Topic: 10. Understanding Selenium Locators - Locator Strategy
# Practice site: https://the-internet.herokuapp.com/
#
# Selenium supports multiple locator strategies: ID, Name, Class, Tag, Link
# Text, Partial Link Text, CSS Selector, and XPath.


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

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

        by_id = driver.find_element(By.ID, "content")
        by_class = driver.find_element(By.CLASS_NAME, "heading")
        by_tag = driver.find_element(By.TAG_NAME, "h1")
        by_link = driver.find_element(By.LINK_TEXT, "Form Authentication")
        by_partial = driver.find_element(By.PARTIAL_LINK_TEXT, "Form Auth")
        by_css = driver.find_element(By.CSS_SELECTOR, "#content")
        by_xpath = driver.find_element(By.XPATH, "//div[@id='content']")

        assert by_id.is_displayed()
        assert by_class.is_displayed()
        assert by_tag.is_displayed()
        assert by_link.is_displayed()
        assert by_partial.is_displayed()
        assert by_css.is_displayed()
        assert by_xpath.is_displayed()

    finally:
        driver.quit()

Output

Chrome launched successfully.

Website opened successfully.

Element located using ID.

Element located using Class Name.

Element located using Tag Name.

Element located using Link Text.

Element located using Partial Link Text.

Element located using CSS Selector.

Element located using XPath.

All elements found successfully.

Assertions 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 Website

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

Opens the Selenium practice website.


Locate Element Using ID

by_id = driver.find_element(By.ID, "content")

Finds the element using its id attribute.


Locate Element Using Class Name

by_class = driver.find_element(By.CLASS_NAME, "heading")

Finds the element using its CSS class.


Locate Element Using Tag Name

by_tag = driver.find_element(By.TAG_NAME, "h1")

Finds the first <h1> element.


Locate Element Using Link Text

by_link = driver.find_element(
    By.LINK_TEXT,
    "Form Authentication"
)

Finds a hyperlink using its exact visible text.


Locate Element Using Partial Link Text

by_partial = driver.find_element(
    By.PARTIAL_LINK_TEXT,
    "Form Auth"
)

Finds a hyperlink using part of its visible text.


Locate Element Using CSS Selector

by_css = driver.find_element(
    By.CSS_SELECTOR,
    "#content"
)

Finds the element using a CSS selector.


Locate Element Using XPath

by_xpath = driver.find_element(
    By.XPATH,
    "//div[@id='content']"
)

Finds the element using an XPath expression.


Verify All Elements

assert by_id.is_displayed()

assert by_class.is_displayed()

assert by_tag.is_displayed()

assert by_link.is_displayed()

assert by_partial.is_displayed()

assert by_css.is_displayed()

assert by_xpath.is_displayed()

Verifies that every located element is visible on the webpage.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.


Browser Execution Flow

Launch Chrome
      │
      ▼
Open Website
      │
      ▼
Locate Element Using ID
      │
      ▼
Locate Element Using Class Name
      │
      ▼
Locate Element Using Tag Name
      │
      ▼
Locate Element Using Link Text
      │
      ▼
Locate Element Using Partial Link Text
      │
      ▼
Locate Element Using CSS Selector
      │
      ▼
Locate Element Using XPath
      │
      ▼
Verify All Elements
      │
      ▼
Assertions Pass
      │
      ▼
Close Browser

Locator Strategy Priority

In most Selenium projects, the recommended priority is:

PriorityLocator
1ID
2Name
3CSS Selector
4Link Text
5Partial Link Text
6Class Name
7Tag Name
8XPath (when other locators are not suitable)

Automation Testing Example

Suppose you are automating an e-commerce login page.

Selenium needs to:

  • Locate the username textbox using ID.

  • Locate the password textbox using Name.

  • Locate the Login button using a CSS Selector.

  • Locate the Forgot Password link using Link Text.

Different elements can use different locator strategies depending on the available HTML attributes.


Real-World Example

Consider an online banking application.

During automation:

  • Username field → ID

  • Password field → Name

  • Login button → CSS Selector

  • Logout link → Link Text

  • Account summary → XPath

Using the most appropriate locator strategy for each element results in stable and maintainable automation scripts.


Common Mistakes Beginners Make

Always Using XPath

XPath is powerful but often slower and more difficult to maintain than ID or CSS Selector.

Whenever possible, prefer ID, Name, or CSS Selector.


Using Dynamic Attributes

Avoid locators that contain values changing every page load, such as randomly generated IDs.


Using Tag Name for Multiple Elements

By.TAG_NAME often returns the first matching element, which may not be the one you intend to interact with.


Best Practices

  • Prefer ID whenever available.

  • Use Name if ID is unavailable.

  • Prefer CSS Selector over XPath when possible.

  • Use XPath only when necessary.

  • Avoid dynamic attributes.

  • Keep locators simple, readable, and unique.

  • Verify elements are displayed before interacting with them.


Conclusion

Locator Strategies are the foundation of Selenium automation. They determine how Selenium identifies elements within the HTML DOM. Choosing the correct locator strategy results in faster, more reliable, and easier-to-maintain automation scripts. Mastering all locator strategies is essential before moving on to advanced Selenium concepts such as dynamic XPath, CSS Selectors, waits, and Page Object Model.


Frequently Asked Questions (FAQs)

What is a locator strategy?

A locator strategy is the method Selenium uses to identify and locate elements on a webpage.


Which locator strategy is the fastest?

ID is generally the fastest and most reliable because it is designed to be unique.


Which locator should I use if no ID is available?

Use Name or CSS Selector if available. Use XPath only when other locator strategies are not suitable.


Can multiple locator strategies locate the same element?

Yes. The same element can often be located using ID, Class Name, CSS Selector, or XPath.


Which locator strategy is most commonly used in real projects?

The most common order is ID, Name, CSS Selector, and then XPath when required.


Key Takeaways

  • Locator strategies tell Selenium how to identify web elements.

  • Selenium provides eight built-in locator strategies.

  • ID is usually the preferred locator because it is fast and reliable.

  • Different elements may require different locator strategies depending on the HTML structure.

  • Avoid using dynamic or unstable attributes in locators.

  • Choosing the right locator strategy improves automation stability and maintainability.

  • Locator strategies are a fundamental Selenium concept and one of the most frequently asked interview topics.