Introduction
Writing a locator that successfully identifies a web element is only the first step in Selenium automation. Writing locators that remain stable, readable, and maintainable across multiple application releases is what distinguishes professional automation engineers from beginners.
Many automation failures occur not because Selenium is unreliable, but because poorly designed locators break after minor UI changes. Common mistakes include using brittle Absolute XPath expressions, relying on dynamically generated attributes, writing overly complex locators, and choosing inappropriate locator strategies.
In this tutorial, you’ll learn the most common locator mistakes made by beginners, practical examples, real-world use cases, and best practices followed in professional automation frameworks.
Why Should You Avoid Common Locator Mistakes?
Avoiding common locator mistakes helps you:
Improve automation reliability.
Reduce framework maintenance.
Write cleaner automation scripts.
Improve locator readability.
Reduce false test failures.
Build scalable automation frameworks.
Common Locator Mistake 1: Using Absolute XPath Unnecessarily
Avoid
driver.find_element(
By.XPATH,
"/html/body/div/div/form/div/input"
)
Absolute XPath expressions are:
Difficult to maintain.
Sensitive to UI changes.
Difficult to read.
More likely to break after minor webpage modifications.
Prefer
driver.find_element(
By.ID,
"username"
)
or
driver.find_element(
By.CSS_SELECTOR,
"#username"
)
Always prefer stable and maintainable locator strategies whenever possible.
Common Locator Mistake 2: Using Multiple Class Names with By.CLASS_NAME
Many beginners attempt to write:
Incorrect
driver.find_element(
By.CLASS_NAME,
"radius success"
)
This will fail because:
By.CLASS_NAME accepts only
a single class name.
Correct
driver.find_element(
By.CSS_SELECTOR,
".radius.success"
)
or
driver.find_element(
By.CSS_SELECTOR,
"button[type='submit']"
)
CSS Selectors provide significantly more flexibility when multiple classes are present.
Common Locator Mistake 3: Depending Upon Dynamic Attributes
Avoid
<input id="user_164782">
and:
driver.find_element(
By.ID,
"user_164782"
)
If the ID changes during every execution, the locator will fail.
Prefer
driver.find_element(
By.CSS_SELECTOR,
"button[type='submit']"
)
or:
driver.find_element(
By.XPATH,
"//input[contains(@id,'user')]"
)
Stable attributes significantly improve framework maintainability.
Common Locator Mistake 4: Writing Overly Complex Locators
Avoid
body div.container
div.content form
div input
Prefer
#username
or
button[type='submit']
Simple locators are:
Easier to read.
Easier to maintain.
Less likely to fail after UI changes.
Common Locator Mistake 5: Ignoring Better Locator Strategies
Professional automation frameworks generally follow this order:
ID
│
▼
Name
│
▼
CSS Selector
│
▼
XPath
│
▼
Relative Locator
│
▼
Use When Needed
Always choose the simplest locator that uniquely identifies the required element.
Example
The Login page contains a Username textbox and a Login button. Selenium demonstrates how stable locator strategies significantly improve automation reliability while avoiding common mistakes.
The Selenium code is:
from selenium import webdriver
from selenium.webdriver.common.by import By
# Topic: 15. Locator Best Practices - Common Locator Mistakes
# Practice site: https://the-internet.herokuapp.com/login
# Run: pytest -s 15_examples/test_04_common_locator_mistakes.py
#
# Common mistakes: using brittle absolute XPath, relying on auto-generated class
# names, and using class name when multiple classes are present.
def test_avoid_common_locator_mistakes():
driver = webdriver.Chrome()
try:
driver.get("https://the-internet.herokuapp.com/login")
# Mistake: class name with multiple classes
# driver.find_element(
# By.CLASS_NAME,
# "radius success"
# )
# Correct: use ID or CSS
username = driver.find_element(
By.ID,
"username"
)
submit = driver.find_element(
By.CSS_SELECTOR,
"button[type='submit']"
)
assert username.is_displayed()
assert submit.is_displayed()
finally:
driver.quit()
Output
The Username textbox and
the Login button are
located successfully using
stable and maintainable
locator strategies.
Understanding the Code
Import the By Class
from selenium.webdriver.common.by import By
Imports Selenium’s locator strategies.
Open the Login Page
driver.get(
"https://the-internet.herokuapp.com/login"
)
Launches the Selenium practice website.
Incorrect Approach
driver.find_element(
By.CLASS_NAME,
"radius success"
)
This approach fails because Selenium expects only a single class name when using By.CLASS_NAME.
Locate the Username Field
driver.find_element(
By.ID,
"username"
)
Uses a stable and highly reliable ID locator.
Locate the Login Button
driver.find_element(
By.CSS_SELECTOR,
"button[type='submit']"
)
Uses a readable and maintainable CSS Selector to identify the Login button.
Validate the Elements
assert username.is_displayed()
assert submit.is_displayed()
Verifies that both elements are successfully located.
How Common Locator Mistakes Affect Automation
Python Script
│
▼
Choose Locator Strategy
│
┌───────┴────────┐
▼ ▼
Poor Locator Stable Locator
│ │
▼ ▼
Frequent Failures Reliable Tests
│ │
▼ ▼
Increased Maintenance Better Scalability
│ │
└───────┬────────┘
▼
Maintainable Framework
Practical Example
Suppose you’re automating an E-Commerce website.
The Checkout button contains multiple CSS classes that frequently change during deployments.
Instead of writing:
By.CLASS_NAME
with multiple class names, Selenium uses:
By.CSS_SELECTOR
with stable attributes to improve locator reliability.
This significantly reduces automation maintenance across multiple releases.
Automation Testing Example
Consider an online banking application.
Large automation frameworks may contain thousands of locators. Using brittle Absolute XPath expressions and unstable attributes significantly increases maintenance effort during regression testing.
Professional automation frameworks prioritize:
Stable IDs.
CSS Selectors.
Readable XPath expressions.
Maintainable locator strategies.
Appropriate Relative Locators when necessary.
Following these practices significantly improves automation reliability across environments.
Real-World Example
Automation engineers frequently avoid common locator mistakes while working with:
Banking applications.
E-Commerce websites.
Healthcare portals.
CRM systems.
ERP applications.
SaaS products.
Enterprise web applications.
Dynamic dashboards.
Modern JavaScript-based applications.
Stable locator strategies significantly improve framework scalability and long-term maintainability.
Advantages of Following Locator Best Practices
Improves automation reliability.
Reduces framework maintenance.
Produces cleaner automation scripts.
Improves locator readability.
Reduces false automation failures.
Improves framework scalability.
Common Mistakes Beginners Make
Using Absolute XPath Everywhere
Always prefer simpler locator strategies whenever possible.
Using Multiple Class Names with By.CLASS_NAME
Remember that:
By.CLASS_NAME accepts
only one class name.
Use CSS Selectors when multiple classes are present.
Depending Upon Dynamic IDs
Avoid dynamically generated IDs whenever stable alternatives are available.
Writing Overly Complex Locators
Keep locator expressions:
Short
Readable
Maintainable
Ignoring Stable Attributes
Always check whether:
ID
Name
CSS Selectors
can uniquely identify the required element before writing complex locators.
Best Practices
Prefer stable IDs whenever available.
Use CSS Selectors for readable and maintainable locators.
Avoid brittle Absolute XPath expressions.
Keep locator expressions short and simple.
Verify locators using browser Developer Tools.
Use appropriate locator strategies based on the application’s requirements.
Prioritize maintainability over unnecessary complexity.
Conclusion
Most Selenium automation failures originate from poorly designed locator strategies rather than problems with Selenium itself. By avoiding common locator mistakes and following industry best practices, automation engineers can build scalable, reliable, and maintainable automation frameworks.
Professional automation frameworks always prioritize simplicity, stability, and readability when designing locator strategies. Choosing the right locator today can save significant maintenance effort in the future.
Frequently Asked Questions (FAQs)
What is the most common locator mistake?
Using brittle and overly complex locator strategies such as long Absolute XPath expressions is one of the most common mistakes.
Can I use multiple class names with By.CLASS_NAME?
No.
By.CLASS_NAME accepts only a single class name. Use CSS Selectors when multiple classes are present.
Should I avoid XPath completely?
No.
XPath is extremely useful for:
Dynamic elements.
Text-based matching.
DOM navigation.
XPath Axes.
Choose the locator strategy that best suits the application’s requirements.
Which locator strategy should I prefer?
Professional automation frameworks generally follow this order:
ID
Name
CSS Selector
XPath
Relative Locator (when necessary)
Why are stable locators important?
Stable locators:
Improve automation reliability.
Reduce maintenance effort.
Improve framework scalability.
Reduce false test failures.
Key Takeaways
Avoid brittle and overly complex locator strategies.
Prefer stable IDs and CSS Selectors whenever possible.
By.CLASS_NAMEaccepts only a single class name.Keep locator expressions short, readable, and maintainable.
Choose the simplest locator strategy that uniquely identifies an element.
Professional automation frameworks prioritize reliability and maintainability over unnecessary complexity.
