The SDET Playbook

← All questions

What is the best locator strategy: CSS, XPath, test IDs or role-based selectors?

Asked Sep 28, 2026Viewed 0 times

1 Answer

Sign in to answer and to vote.

  • 0
    The SDET PlaybookSep 28, 2026

    Prioritize what users see and explicit contracts over DOM structure. Playwright recommends role-based locators (for example a button by its accessible name) and warns that CSS class chains and XPath break when designers change markup. Its code generator ranks role, text and test-id locators first, and it suggests chaining and filtering to narrow to part of the page. A practical order is role or label, then a dedicated test id where no accessible name exists, then CSS, with XPath as a last resort. Role-based locators also nudge the team toward accessible markup.

    Sources: Playwright best practices