Introduction to Locators

Locators tell your automation tool which element to interact with — click a button, type in an input, assert text, and more.

Why locators matter

Every Selenium, Playwright, Cypress, or Appium (web) script finds elements before it can act on them. A wrong or brittle locator makes tests fail even when the application works correctly.

What you will learn here

  • All major locator strategies used in test automation
  • How to build CSS selectors and XPath expressions
  • How to locate inputs, buttons, dropdowns, tables, links, and more
  • How to handle iframes, Shadow DOM, and dynamic elements
  • Syntax differences across Selenium, Playwright, and Cypress
  • Best practices so locators stay stable

Locator strategies overview

Strategy Example Best for
ID #login-btn Unique, stable elements
Name [name="email"] Form fields
Class Name .btn-primary Styled groups (use carefully)
Tag Name button Broad matches, rarely alone
Link Text Forgot Password? Exact link text
Partial Link Text Forgot Partial link text
CSS Selector button.btn-primary Fast, readable, most cases
XPath //button[text()='Submit'] Text, hierarchy, complex logic
data-testid [data-testid="submit"] Test-friendly (recommended)

Use the left menu to study each topic. Hands-on UI practice is on the main Practice Hub page.