Playwright + TS · Lesson 17 of 36

0/36 done

Module 17: Locators (Deep Dive)

17. User-First Locators & Resilient Selection Strategies

20 minlesson

Try it yourself

Implement buildRowDeleteLocator:

  1. Take rowText parameter.
  2. Return a selector matching the table row with rowText and targeting its delete button.

The editor has a worked example. Read it, tweak it, then press Mark as complete.

Playwright recommends prioritizing locators in this order:

  1. `page.getByRole(role, { name })`: The gold standard. Tests accessibility and matches screen-reader semantics.
  2. `page.getByLabel('Username')`: Best for form inputs with linked <label> elements.
  3. `page.getByPlaceholder('Enter email')`: Best when no visible label exists.
  4. `page.getByText('Submit Order')`: Locates non-interactive text elements.
  5. `page.getByTestId('checkout-btn')`: Dedicated QA test id (data-testid).
  6. `page.locator('css-or-xpath')`: Fallback when accessibility locators are unfeasible.

Strict Mode Violation

Playwright enforces strict mode on all locators by default! If your locator matches more than one element, Playwright will throw an error:

typescript
// ❌ If there are 3 buttons matching "Delete", this throws StrictModeError:
await page.getByRole("button", { name: "Delete" }).click();

// ✅ Solution 1: Filter to pinpoint specific row
await page.locator("tr", { hasText: "Order #402" })
  .getByRole("button", { name: "Delete" })
  .click();

// ✅ Solution 2: Filter by child locator
await page.getByRole("listitem")
  .filter({ has: page.getByRole("heading", { name: "MacBook Pro" }) })
  .getByRole("button", { name: "Buy" })
  .click();

// ✅ Solution 3: Exact index (first / last / nth)
await page.getByRole("button", { name: "Delete" }).first().click();

Locator Chaining

Locators can be chained seamlessly:

typescript
const nav = page.getByRole("navigation");
const loginLink = nav.getByRole("link", { name: "Sign in" });
await loginLink.click();

Key Takeaways & Walkthrough Summary

Locators are the foundation of reliable test automation. Playwright prioritizes user-facing, accessible locators like getByRole and getByLabel over fragile CSS or XPath selectors. In this deep dive, you will learn how to locate elements the way real users see them, handle strict mode violations, and filter dynamic lists with zero flakiness. Let's master locators!

Part of the Playwright + TS course (Playwright with TypeScript). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

Why is getByRole preferred over getByTestId?

getByRole ensures your app remains accessible to users with screen readers and disability tools. If an engineer forgets a label or uses a div instead of a button, getByRole fails, catching accessibility bugs automatically.

What is the difference between page.$ and page.locator?

page.$ is legacy Puppeteer-style; it queries the DOM at a single instant without auto-waiting. page.locator returns a resilient locator that evaluates actionability dynamically whenever an action is called.

Finished reading?

Review the worked example in the editor, then mark the lesson complete.

TypeScript
Skip