SoftwareTestPilot
All discussions
Jonas HansenJul 10

Which Selenium locator strategy is most reliable?

id vs css vs xpath vs data-testid — team keeps arguing. What actually holds up on a modern SPA?

seleniumlocators
Related learningExplore Academy skill paths

1 answer

Accepted solution

Jonas HansenJul 11

Order of preference on a modern SPA: 1) data-testid (or another test-only attribute), 2) id when it is stable, 3) CSS selector, 4) XPath only when you truly need text or DOM traversal. Never rely on class chains generated by CSS-in-JS — they change on every deploy.