Locator Techniques and Tools: Identifying Objects in Selenium WebDriver

Akshada AutiAkshada Auti
3 min read

Here’s a complete blog-style notes article based on Section 5: Locator Techniques & Tools used to identify Objects. You can copy and use this in your notes, Hashnode blog, or GitHub repository.


🔍 Locator Techniques & Tools in Selenium WebDriver – A Practical Guide

When working with Selenium, locating elements accurately is one of the most crucial skills every tester must master. This blog covers the most commonly used locator strategies and introduces helpful tools like SelectorsHub that simplify the process.


✅ 1. Why Locators Are So Important in Selenium

In Selenium WebDriver, everything starts with locating a web element:

  • Clicking a button

  • Typing in a text box

  • Validating a label or heading

Without a solid locator, your automation becomes unreliable. So choosing the right locator strategy is the foundation of robust test cases.


🧭 2. Types of Locators in Selenium (Basic)

➤ ID Locator

driver.findElement(By.id("username"));
  • Fastest and most reliable

  • Must be unique on the page


➤ Name Locator

driver.findElement(By.name("email"));
  • Common in forms

  • May not be unique


🎯 3. Using Class Name and CSS Selectors

➤ Class Name

driver.findElement(By.className("btn-primary"));
  • Works if the class is single (won’t work well with multiple class names)

➤ CSS Selector

driver.findElement(By.cssSelector("input[type='email']"));
  • Fast, readable, supports partial matches:

    • ^= → starts with

    • $= → ends with

    • *= → contains


🧰 4. Using Browser Plugin: SelectorsHub

SelectorsHub is a browser extension to generate:

  • XPath

  • CSS Selectors

  • Shadow DOM paths

  • iframe-aware locators

🎯 Why use it?

  • It suggests robust and optimized locators

  • It highlights any syntax issues

  • Ideal for manual testers working with automation teams


🧠 5. XPath Techniques – text() & contains()

➤ Direct Match

//button[text()='Login']

➤ Partial Match

//a[contains(text(),'Sign')]

Useful when elements change frequently or support multiple languages.


🧪 6. Custom XPath & CSS Based on Attributes

When elements don’t have reliable IDs, use custom attributes:

//input[@data-testid='email']
input[data-testid='email']

These are common in React/Angular-based apps using testing IDs.


🔗 7. XPath: Parent-to-Child and Child-to-Parent Traversal

➤ From Parent to Child

//div[@class='form-group']/input

➤ From Child to Parent

//input[@id='password']/parent::div

Useful in identifying context (e.g., error messages or labels).


📌 Key Takeaways

ConceptDescription
id and nameFirst choice for simplicity and speed
className, CSSPowerful for grouped elements
XPathBest for complex structures, nested elements
SelectorsHubTool to visually test and generate locators
Parent/Child TraversalHelps build context-aware XPath expressions

✨ Final Thought

Understanding locator techniques will save you hours of debugging in Selenium. Combine browser tools like DevTools + SelectorsHub to write robust test scripts or help your automation team with reliable locators.

0
Subscribe to my newsletter

Read articles from Akshada Auti directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Akshada Auti
Akshada Auti