Built-in Locators in Playwright

From the Playwright cheat sheet ยท Locators ยท verified Jul 2026

Built-in Locators

Use role-based, text, and semantic locators to find elements

typescript
// By role (preferred โ€” accessible and resilient)
page.getByRole('button', { name: 'Submit' })
page.getByRole('heading', { name: 'Welcome' })
page.getByRole('link', { name: 'Home' })

// By label, placeholder, text
page.getByLabel('Email')
page.getByPlaceholder('Search...')
page.getByText('Hello World')

// By test ID
page.getByTestId('submit-btn')
๐Ÿ’ก Prefer getByRole over CSS selectors โ€” it mirrors how users and assistive tech see the page
โšก Locators auto-wait and auto-retry โ€” no need for explicit waitFor calls
๐Ÿ“Œ Use filter() to narrow down results from broad locators like getByRole("listitem")
๐ŸŸข getByTestId uses data-testid by default โ€” customizable in playwright.config.ts
locatorsselectors
Back to the full Playwright cheat sheet