Master Selenium Locators: ID, Class, Name, Xpath & More

Introduction to Selenium Locators

Selenium WebDriver is the most popular open-source tool for automating web browsers, used by QA engineers and developers worldwide. With support for multiple programming languages (Java, Python, C#, Ruby), it enables efficient web automation testing.

One of the most critical skills in Selenium automation is locating web elements accurately. This guide covers all Selenium locators, their best use cases, and pro tips to enhance your test automation framework.

Why Are Locators Important in Selenium?

Locators help Selenium identify and interact with web elements like:

  • Buttons 🖱️

  • Text fields 📝

  • Dropdowns 🔽

  • Checkboxes ☑️

  • Links 🔗

Using the right locator strategy improves:
✅ Test stability (fewer broken scripts)
✅ Execution speed (faster element finding)
✅ Maintainability (easier script updates)


8 Types of Selenium Locators (With Examples)

1. ID Locator (Most Preferred)

Finds elements using their unique HTML ID.

java

 
 WebElement element = driver.findElement(By.id("username"));

✅ Best for: Login fields, buttons with static IDs

⚠️ Avoid if: IDs are dynamically generated


2. Name Locator

Locates elements by their name attribute.

python


 element = driver.find_element(By.NAME, "email")

✅ Best for: Forms with multiple fields (e.g., registration forms)


3. Class Name Locator

Finds elements by CSS class.

java

 
 List<WebElement> buttons = driver.findElements(By.className("btn-primary"));

⚠️ Caution: Multiple elements may share the same class


4. Tag Name Locator

Selects elements by HTML tag (e.g., <a><div>).

python


 links = driver.find_elements(By.TAG_NAME, "a")

✅ Best for: Counting links or buttons on a page


5. Link Text & Partial Link Text

Exact match:


java

 
 driver.findElement(By.linkText("Forgot Password?"));


Partial match:


python


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

✅ Best for: Navigation menus, footer links


6. CSS Selector (Advanced)

Powerful locator using CSS query syntax.

java


 WebElement button = driver.findElement(By.cssSelector("button.submit[type='button']"));

Pro Tip: Use Chrome DevTools (Ctrl+Shift+C) to copy CSS selectors.


7. XPath (Most Flexible)

Supports complex traversals (parent/child/sibling).

python

 
 element = driver.find_element(By.XPATH, "//input[@name='search']")

Types:

  • Absolute XPath (/html/body/div[...]) → Fragile ❌

  • Relative XPath (//button[contains(text(),'Submit')]) → Preferred ✅

How to find XPath in Chrome:

  1. Right-click element → Inspect

  2. Right-click HTML → Copy → Copy XPath

Locator Best Practices 

1. Prefer ID > CSS > XPath for performance

2. Avoid absolute XPath (breaks easily)

3. Use explicit waits with locators:


java


 new WebDriverWait(driver, 10).until(
    ExpectedConditions.visibilityOfElementLocated(By.id("submit"))
 );


4. Customize dynamic IDs with partial matches:


python


 driver.find_element(By.XPATH, "//div[contains(@id,'prod_')]")


Practical Example: Techlistic Practice Form

URL: https://www.techlistic.com/p/selenium-practice-form.html


Element
Locator Example
First Name FieldBy.id("firstName")
Gender RadioBy.cssSelector("input[value='Male']")
Submit ButtonBy.xpath("//button[text()='Submit']")

FAQ: Selenium Locators

❓ Which locator is fastest?
→ ID locator (browser-optimized).

❓ How to handle dynamic elements?
→ Use XPath/CSS functions like contains()starts-with().

❓ Can I use multiple locators together?
→ Yes! Combine them in Relative XPath/CSS:

css

 
 div.form-container > input.username


Conclusion

Mastering Selenium locators is essential for robust test automation. Key takeaways:
✔ Use IDs where available
✔ CSS Selectors for complex queries
✔ Relative XPath for dynamic content
✔ Always validate locators in DevTools


What is Selenium WebDriver?  << Previous      ||      Next >>  Selenium WebDriver Commands

Comments

Popular posts from this blog

Selenium Automation for E-commerce Websites

Top 10 Demo Websites for Selenium Automation Practice

Mastering Selenium Practice: Automating Web Tables with Demo Examples

Top 10 Highest Paid Indian-Origin CEOs in the USA: Leaders Shaping Industries

25+ Selenium WebDriver Commands: The Complete Cheat Sheet with Examples

14 Best Selenium Exercises to Master Automation Testing Skills

Understanding Cryptocurrency: A Beginner's Guide to Bitcoin and Ethereum

Best Budget Tech Gadgets You Can Buy in 2025

Top 8 AI Tools You Should Start Using Today

How to Automate Google Search with Selenium WebDriver