Selectors — Finding Elements
Master Playwright's selector strategies. Learn getByRole, getByText, getByLabel, getByTestId, and CSS selectors -- and when to use each one.
Playwright Automation • Module 2 • Lesson 1
12 min read
What you'll learn
- Use getByRole, getByText, getByTestId, and getByLabel to locate elements
- Choose the right selector strategy for each situation
- Understand why role-based selectors are preferred
Playwright AutomationlessonsJump to another lesson
Locators: Finding Elements
getByRole1st choice
getByText2nd
getByLabel3rd
getByTestId4th
CSS / XPath5th
- getByRole:Accessible + resilient
- getByText:Visible text content
- getByLabel:Form fields with labels
- getByTestId:Escape hatch
- CSS / XPath:Last resort, brittle
The 5 Selector Strategies
Quick Check
Question 1 of 10 correct
Which selector should you reach for FIRST for a '<button>Submit</button>'?
Why getByRole Wins
Avoid CSS/XPath as First Choice
Selector Priority Summary
| Priority | Strategy | Best For | Resilience |
|---|---|---|---|
| 1st | getByRole | Buttons, links, headings, form elements | Highest — tests accessibility |
| 2nd | getByText | Messages, labels, static text | High — text rarely changes |
| 3rd | getByLabel | Form fields with labels | High — labels are user-visible |
| 4th | getByTestId | Elements with no semantic hook | Medium — stable but not semantic |
| 5th | CSS / XPath | Legacy code, complex DOM queries | Low — breaks on refactors |
Practice: Match HTML to the Best Selector
Match each HTML snippet on the left with the best Playwright selector on the right.
Practice: Fill In the Selectors
Fill in the correct Playwright locator method for each interaction.
import { test, expect } from '@playwright/test'; test('user can log in', async ({ page }) => { await page.goto('https://app.example.com/login'); // Fill the "Email" input field (has a <label>Email</label>) await page.('Email').fill('user@test.com'); // Fill the "Password" input field (has a <label>Password</label>) await page.('Password').fill('secret123'); // Click the "Sign In" button await page.('button', { name: 'Sign In' }).click(); // Verify the welcome heading appears await expect( page.('heading', { name: 'Welcome' }) ).toBeVisible(); });
Try It: Log In with Selectors
Knowledge Check
Question 1 of 30 correct