75 / 139 · 13 Browser Automation with Playwright · Locator Strategies← prev⊞ allnext →☰ Read as one page
7.6Key Takeaways
- Prefer role-based locators (
getByRole) — they mirror user behavior and survive refactoring - Use
getByTestIdwhen semantic locators are not available — it creates a stable test contract - Reserve CSS/XPath for third-party widgets and legacy applications
- Avoid positional selectors, deep nesting, and styling-based class selectors
- A good locator strategy reduces test maintenance by 60–80% compared to CSS/XPath-heavy tests