Playwright · Junior · Question 71 of 75
Why is getByRole('link', { name: 'Docs' }) better than locator('a:nth-child(3)')?
- 1It runs faster because it skips the DOM tree
- 2It doesn't depend on element order or markup
- 3It works even when the link is not rendered
Show the answer
Correct answer: It doesn't depend on element order or markup
Explanation
Positional selectors break when someone adds a link. A role and accessible name describe what the user sees.