Playwright · Junior · Question 14 of 75
Which locator does Playwright recommend first for a button?
- 1
page.locator('//div[3]/button[1]') - 2
page.locator('#root > div > button.btn-2') - 3
page.getByRole('button', { name: 'Save' })
Show the answer
Correct answer: page.getByRole('button', { name: 'Save' })
Explanation
Role locators reflect how users and assistive tech see the page. They survive markup and class changes better than CSS or XPath chains.