Testing / Playwright Interview questions
List the different types of locators in Playwright?
Playwright encourages user-facing, role-based locators over brittle CSS/XPath selectors wherever possible.
| Locator | Matches by |
getByRole() | ARIA role and accessible name |
getByText() | Visible text content |
getByLabel() | Associated form label |
getByPlaceholder() | Input placeholder text |
getByAltText() | Image alt attribute |
getByTitle() | title attribute |
getByTestId() | data-testid attribute |
page.locator() | Raw CSS or XPath |
Role and text-based locators are preferred because they mirror how a real user or assistive technology identifies elements, so they survive styling and markup changes better than a CSS class chain.
All of these are methods on page or on another locator, so they can be chained - for example scoping to a row before finding a button inside it - and combined with filters like .filter({ hasText: '...' }) to narrow down matches further.
More Related questions...