Introduction
Selenium provides multiple locator strategies to identify web elements, such as ID, Name, Class Name, Tag Name, Link Text, Partial Link Text, CSS Selector, and XPath. While all of these locators can be used to locate elements, some are more reliable and maintainable than others.
Choosing the correct locator priority helps create stable automation scripts that are less likely to fail when the application’s user interface changes. Automation engineers should always prefer the most reliable locator available instead of immediately using complex XPath expressions.
In this tutorial, you will learn what locator priority is, why it is important, the recommended locator order, practical examples, real-world applications, common mistakes, best practices, and frequently asked interview questions.
What is Locator Priority?
Locator Priority refers to the recommended order of choosing Selenium locators based on their reliability, uniqueness, readability, and maintainability.
Rather than randomly selecting a locator, automation engineers should always choose the highest-priority locator that uniquely identifies the element.
Following a consistent locator priority makes automation frameworks more robust and easier to maintain.
Why is Locator Priority Important?
Using the correct locator priority helps to:
-
Improve automation stability.
-
Reduce flaky test cases.
-
Minimize script maintenance.
-
Improve test execution reliability.
-
Make automation scripts easier to understand.
-
Reduce failures caused by UI changes.
Recommended Locator Priority
The generally accepted locator priority in Selenium is:
| Priority | Locator | Recommendation |
|---|---|---|
| 1 | ID | Best choice whenever available |
| 2 | Name | Good alternative if ID is unavailable |
| 3 | CSS Selector | Fast, flexible, and highly recommended |
| 4 | XPath | Use when other locators are not available |
| 5 | Link Text | Best for hyperlinks |
| 6 | Partial Link Text | Useful for long hyperlinks |
| 7 | Class Name | Use only if unique |
| 8 | Tag Name | Rarely used alone |
Always use the highest-priority locator that uniquely identifies the required element.
Why is ID the Highest Priority?
The ID locator is considered the most reliable because:
-
IDs are designed to be unique.
-
It is easy to read.
-
It performs efficiently.
-
It rarely changes compared to other attributes.
Example:
driver.find_element(By.ID, "username")
Practical Example
The following example demonstrates the recommended locator priority.
The automation script opens the Selenium practice login page and locates different elements using ID, Name, and CSS Selector, following the recommended priority order.
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 10. Understanding Selenium Locators - Locator Priority
# Practice site: https://the-internet.herokuapp.com/login
#
# Recommended locator priority: ID > Name > CSS Selector > XPath > Link Text.
# Use the highest-priority locator that is unique and stable.
def test_locator_priority_order():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
# Priority 1: ID
username = driver.find_element(By.ID, "username")
# Priority 2: Name
password = driver.find_element(By.NAME, "password")
# Priority 3: CSS Selector
submit = driver.find_element(By.CSS_SELECTOR, "button.radius")
assert username.is_displayed()
assert password.is_displayed()
assert submit.is_displayed()
finally:
driver.quit()
Output
Browser launched successfully.
Login page opened successfully.
Username field located using ID.
Password field located using Name.
Login button located using CSS Selector.
All elements located 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 class.
Launch Chrome Browser
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")
Uses the highest-priority locator because the username field has a unique ID.
Locate Password Using Name
password = driver.find_element(By.NAME, "password")
Uses the Name locator because it uniquely identifies the password field.
Locate Login Button Using CSS Selector
submit = driver.find_element(By.CSS_SELECTOR, "button.radius")
Uses a CSS Selector to identify the Login button.
Validate All Elements
assert username.is_displayed()
assert password.is_displayed()
assert submit.is_displayed()
Verifies that all located elements are visible on the webpage.
Close the Browser
driver.quit()
Closes all browser windows and ends the WebDriver session.
Locator Priority Flow
Inspect the Element
│
▼
Unique ID Available?
│
Yes
│
▼
Use ID
│
No
│
▼
Unique Name Available?
│
Yes
│
▼
Use Name
│
No
│
▼
Use CSS Selector
│
No
│
▼
Use XPath
│
▼
Use Link Text (if applicable)
│
▼
Avoid Weak Locators
Automation Testing Example
Suppose you’re automating an employee management system.
The username field has:
<input id="username" name="username">
Instead of writing:
driver.find_element(By.XPATH, "//input[@id='username']")
Simply use:
driver.find_element(By.ID, "username")
This makes the automation script cleaner and easier to maintain.
Real-World Example
Consider an online banking application.
The Login button contains:
<button class="radius">
Login
</button>
A reliable CSS Selector would be:
driver.find_element(By.CSS_SELECTOR, "button.radius")
If the button had a unique ID, using the ID locator would still be the preferred option.
Common Mistakes Beginners Make
Using XPath for Every Element
Incorrect
driver.find_element(By.XPATH, "//input[@id='username']")
Even though an ID is available, some beginners still use XPath.
Better
driver.find_element(By.ID, "username")
Always use the highest-priority locator.
Ignoring Unique IDs
Many beginners immediately inspect the XPath without checking whether the element already has a unique ID or Name.
Using Class Name for Multiple Elements
Incorrect
driver.find_element(By.CLASS_NAME, "form-control")
If several elements share the same class, Selenium may locate the wrong element.
Best Practices
-
Always follow the recommended locator priority.
-
Prefer ID whenever available.
-
Use Name as the second preference.
-
Use CSS Selectors before XPath whenever possible.
-
Avoid absolute XPath.
-
Verify that the locator uniquely identifies one element.
-
Write simple, readable, and maintainable locators.
Conclusion
Following the recommended locator priority helps build reliable, maintainable, and efficient Selenium automation scripts. Choosing the highest-priority locator reduces test failures and simplifies framework maintenance.
Whenever possible, use ID, followed by Name, CSS Selector, and then XPath. Consistently applying this approach is considered an industry best practice for Selenium automation.
Frequently Asked Questions (FAQs)
What is locator priority in Selenium?
Locator priority is the recommended order of choosing Selenium locators based on their reliability and maintainability.
Which locator has the highest priority?
The ID locator has the highest priority because it is usually unique and stable.
Why is CSS Selector preferred over XPath?
CSS Selectors are generally shorter, easier to read, and often perform better than complex XPath expressions.
Should I always use XPath?
No.
XPath should be used only when higher-priority locators such as ID, Name, or CSS Selector are not available.
Is Class Name a good locator?
It can be used if the class uniquely identifies the element. Otherwise, prefer a higher-priority locator.
Key Takeaways
-
Locator priority helps create stable and maintainable Selenium scripts.
-
The recommended order is ID → Name → CSS Selector → XPath → Link Text.
-
Always choose the highest-priority locator that uniquely identifies an element.
-
Avoid using XPath when a simpler locator is available.
-
Prefer readable and maintainable locators over complex expressions.
-
Following locator priority reduces automation maintenance and test failures.
-
Understanding locator priority is an important Selenium interview topic and an automation best practice.
