Back to All Questions
Question 47 of 100
Locators
Beginner
What is the importance of getByTitle locator in Playwright?
The Answer
`getByTitle` locates elements by their `title` attribute. This is useful for icon buttons (that have no visible text) or elements with tooltip-style titles.
Deep Dive Explanation
The `title` attribute is less common than `aria-label` in modern accessible UIs. Prefer `getByRole('button', { name: 'Close dialog' })` when possible, as ARIA labels are better supported by screen readers. Use `getByTitle` specifically when an element only has a `title` attribute and no accessible role name.
example.spec.ts
// HTML: <button title="Close dialog">X</button>
await page.getByTitle('Close dialog').click();
// HTML: <abbr title="HyperText Markup Language">HTML</abbr>
await expect(page.getByTitle('HyperText Markup Language')).toBeVisible();
// Partial match
await page.getByTitle('Export', { exact: false }).click();