TargetQA
TypeScript
Lesson 2 of 4 · 6 min

1.2 Modern Locators: getByRole & User-Facing Selectors

AI

Lesson Audio Narration

### User-First Locator Philosophy Traditional automation often relied on fragile selectors like: ```typescript // ❌ Brittle: Breaks when CSS classes or DOM structure change page.locator('div.header > ul > li:nth-child(2) > a.btn-primary'); ``` Playwright recommends **user-facing role locators**: ```typescript // ✅ Resilient: Reflects what users see and interact with page.getByRole('button', { name: 'Sign in' }); page.getByRole('heading', { name: 'Welcome back' }); ``` ### Common Roles: - `'button'`: Buttons and clickable submit elements - `'link'`: Navigation links (`<a>` tags) - `'textbox'`: Input fields and textareas - `'heading'`: `<h1>` through `<h6>` headings - `'checkbox'` / `'radio'`: Form selection inputs
Your Task

Locate the submit button with accessible name `'Sign In'` using `page.getByRole('button', { name: 'Sign In' })` and click it.

Common doubts
Did you find this lesson helpful?
solution.ts
Loading code editor…
Execution Console

Click "Run Code" to test your implementation.