Skip to main content

What are your best practices for writing stable CSS/XPath selectors?

  • selectors
  • locators
  • stability

Answer it first, then compare. Nothing here is saved or scored.

Model answer

Priority order: (1) data-testid attributes, most stable, decoupled from styling. (2) ARIA roles and labels (getByRole, getByLabel), user-centric and accessible. (3) Text content (getByText), natural but fragile to copy changes. (4) CSS classes, avoid unless stable and semantic. (5) Never use: auto-generated IDs, position-based selectors (nth-child), deeply nested paths. Work with developers to add test attributes during development. The best selector is one that only changes when the feature changes.

What interviewers listen for

  • Test architecture instincts
    You can defend why your suite is shaped like a pyramid, a trophy, or a honeycomb.
  • Locator discipline
    You rank selector strategies by stability and explain when to break the rule.
  • Flaky test ownership
    You debug to a structural fix instead of retry-until-green.
  • Parallelisation literacy
    You know how your framework isolates state, fixtures, and database access across workers.

Terms to know

See all 35 automation questions

Written by , Senior QA Automation Engineer, 50+ QA candidate interviews conductedLast updated July 2026