Introduction to locators - 6-.Locator Form | Lecture Notes - YNetStudyHub
6-.Locator Form

Introduction to locators

3 mins read 414 words Topic 1 of 10
Study with AI Tutor Learn 2× faster Instant Q&A Quiz practice
Launch AI Lesson
Text Size:
PDF Save

Understanding the concept of locators in Selenium WebDriver and their importance in test automation. Exploring different types of locators such as ID, Name, Class Name, XPath, CSS Selector, etc.

Introduction to Locators
Key concepts for finding and interacting with elements in web‑automation frameworks (e.g., Selenium, Playwright, Cypress).


1. What Is a Locator?

  • Definition: A locator is a selector that uniquely identifies a UI element on a web page (button, link, input, etc.) so that automation scripts can interact with it.
  • Purpose: Enables reliable, maintainable test scripts; abstracts the underlying HTML structure.

2. Why Choose the Right Locator?

Reason Impact of a Poor Locator
Stability Fragile locators break with minor UI changes.
Readability Clear locators make scripts self‑documenting.
Performance Efficient selectors reduce lookup time.
Maintainability Centralized locators simplify updates.

3. Locator Strategies (Common to Most Frameworks)

Strategy Syntax (Selenium‑Python) Typical Use‑Case Pros Cons
ID driver.find_element(By.ID, "username") Unique element IDs Fast, unique Requires developer‑provided IDs
Name driver.find_element(By.NAME, "email") Form fields Simple May not be unique
Class Name driver.find_element(By.CLASS_NAME, "btn-primary") Styling hooks Easy Often shared by many elements
Tag Name driver.find_element(By.TAG_NAME, "button") Generic tag search Useful for collections Too broad alone
Link Text / Partial Link Text driver.find_element(By.LINK_TEXT, "Forgot Password") Anchor links Human‑readable Sensitive to text changes
CSS Selector driver.find_element(By.CSS_SELECTOR, "input[type='email']") Any CSS pattern Very flexible, fast Complex syntax can be hard to read
XPath driver.find_element(By.XPATH, "//div[@class='error']/span") Hierarchical navigation Powerful, can locate any element Slower, brittle if structure changes
Custom Attribute driver.find_element(By.CSS_SELECTOR, "[data-test='login-btn']") Test‑specific attributes Stable, independent of UI Requires dev cooperation

4. Best Practices for Choosing Locators

  1. Prefer IDs – they are unique and fastest.
  2. Use meaningful custom attributes (data-test, data-qa, data-cy) when IDs are unavailable.
  3. Avoid absolute XPaths – they depend on the full DOM hierarchy. Use relative XPaths instead.
  4. Keep selectors short but specific – e.g., button.submit is better than div > form > button.
  5. Leverage CSS over XPath when possible (generally faster and more readable).
  6. Encapsulate locators in a dedicated object‑repository or page‑object class.
  7. Document intent – add comments or descriptive variable names (loginButton, errorMessageSpan).

5. Page Object Model (POM) – Organizing Locators

# example: login_page.py
from selenium.webdriver.common.by import By

class LoginPage:
    # Locators
    USERNAME_INPUT = (By.ID, "username")
    PASSWORD_INPUT = (By.ID, "password")
    SUBMIT_BTN    = (By.CSS_SELECTOR, "button[type='submit']")
    ERROR_MSG     = (By.XPATH, "//div[@class='alert']/p")

    # Actions
    def
Topic Concept Map
Ask AI Tutor 24/7 Live

Click any prompt to explore this topic interactively with your AI Tutor:

Explain in simple terms Give me 3 practice exam questions Summarize key principles in 5 points
Unit Syllabus 10 Topics
Introduction to locators
Using ID locators
Upgrade
Working with Name locators
Upgrade
Locating elements by Class Name
Upgrade
XPath locators in Selenium
Upgrade
Leveraging CSS Selectors
Upgrade
Locating elements by Tag Name
Upgrade
Combining locators
Upgrade
Handling dynamic elements
Upgrade
Best practices for using locators
Upgrade