·

8 min

Locator strategies: role, test ID, CSS selector or XPath?

Roman Kirchmeier - Autemos

Roman Kirchmeier - Autemos

QA engineer points at a highlighted button in a web form to choose a stable locator

The most reliable locator strategy for web applications starts with the ARIA role plus accessible name, uses label and text for form fields, and switches to a test ID (data-testid) when text changes or is missing. CSS selectors and XPath stay the exception. That is the order Playwright and Testing Library recommend. Cypress puts the test ID first, and Selenium recommends ID, then CSS, then XPath.

A locator is an expression a test uses to find exactly one element on the screen. It's a typical breaking point in a UI test: when the DOM changes, the test can't find its target anymore. XPaths generated with the ROBULA+ algorithm were about 90% less fragile than absolute XPaths (Leotta et al., 2016).

TL;DR: Start with getByRole plus accessible name, use the label for form fields, and add a test ID like data-testid for changing text. Use CSS selectors and XPath only when nothing else works. Playwright calls test IDs “the most resilient way of testing”, though not user facing. When a locator breaks, every automatic repair should be documented and reviewable.

Locator priority in five steps: role, label or text, test ID, CSS selector, XPath

Figure 1: Recommended locator order based on Playwright docs and Testing Library

Which locator strategies are there, and how do they compare?

Web tests use five common locator families: ARIA role, label or text, test ID, CSS selector and XPath. Playwright ships seven built-in locators from getByRole to getByTestId (Playwright, 2026), and Selenium supports eight traditional strategies from id to xpath (Selenium, 2026).

An ARIA role is a semantic attribute that tells assistive technology what kind of element it is dealing with. The W3C Recommendation WAI-ARIA 1.2 of June 6, 2023 defines the role as the “Main indicator of type” (W3C, 2023).

Locator

Example

Stability

User facing

Shadow DOM (Playwright)

Readability

Typical use

Role

getByRole('button', { name: 'Transfer' })

high, as long as role and name stay

yes

yes

high

buttons, links, dialogs

Label / text

getByLabel('IBAN')

medium, breaks when text changes

yes

yes

high

form fields, static text

Test ID

getByTestId('transfer-submit')

very high

no

yes

high

changing text, multilingual UIs

CSS selector

#transfer-form button.primary

low to medium

no

yes

medium

unique IDs, existing Selenium suites

XPath

//tr[td='IBAN']/td[2]

low to medium

no

no

low

navigating via neighbors, XML

The stability ratings are our reading of the Playwright docs. They call test IDs “the most resilient way of testing” and add a caveat: “testing by test ids is not user facing.” On CSS and XPath, Playwright is blunt: “CSS and XPath are not recommended as the DOM can often change leading to non resilient tests.” (Playwright, 2026).

Web components add the shadow DOM, a hidden DOM tree inside an element. Playwright finds elements inside it with every locator except in two cases: “Locating by XPath does not pierce shadow roots.” and “Closed-mode shadow roots are not supported.” And “Locators are strict”: a click that matches several elements throws an error.

Why do Playwright, Cypress and Selenium recommend different locator strategies?

Playwright, Cypress and Selenium weigh two goals differently in their locator strategy: closeness to the user and independence from markup. Playwright writes “We recommend prioritizing role locators to locate elements” and puts role locators first. Testing Library ranks getByRole at position 1 of 8 queries and getByTestId at position 8 (Testing Library, 2026).

Comparison: Playwright and Testing Library recommend getByRole, Cypress data attributes, Selenium the HTML ID, then CSS

Figure 2: First choice per framework and the Cypress question about text changes

Cypress takes a different line: “Use data-* attributes to provide context to your selectors and isolate them from CSS or JS changes.” In the selector table of its docs, a data-cy attribute is rated “Always”, and role and text get “Depends” (Cypress, 2026).

Selenium puts unique, predictable HTML IDs first. Without them: “a well-written CSS selector is the preferred method of locating an element.” XPath comes after that (Selenium, 2026).

Framework

First choice

Test ID

CSS selector / XPath

Playwright

getByRole

“most resilient”, not user facing

“not recommended”

Testing Library

getByRole (1 of 8)

position 8, only if role or text don't fit

not in the priority list

Cypress

data-* attribute (“Always”)

first choice

#id “Sparingly”, classes “Never”

Selenium

unique HTML ID

reachable via CSS attribute selector

CSS before XPath

A question from the Cypress docs settles the conflict: “If the content of the element changed would you want the test to fail?” If yes, use role or text. If no, use a test ID. On a payment form, you find the “Transfer” button by role, as its text is part of the requirement. The changing account balance gets a test ID. Tool details are in Playwright vs. Cypress.

What does research say about locator stability?

Peer-reviewed studies show a clear pattern: absolute XPaths break most often, and carefully generated locators last much longer. The less a locator depends on its position in the DOM, the longer it lasts. The numbers come from research prototypes, not from your project. Four papers matter when you pick a locator strategy:

Research figures: ROBULA 56% and 41%, ROBULA+ about 90% and 63% less fragile, Similo 91 of 801 failures versus 214

Figure 3: Locator stability in research (Leotta et al. 2014, 2016; Nass et al. 2023)

  • ROBULA (2014): The algorithm generates XPaths automatically; fragility dropped on average by 56% against absolute and by 41% against relative locators (Leotta et al., ISSREW, 2014).

  • ROBULA+ (2016): The successor cut fragility by about 90% against absolute XPaths and by about 63% against Selenium IDE locators (Leotta et al., JSEP, 2016).

  • Similo (2023): This similarity-based approach failed on 48 popular websites in 91 of 801 cases (about 11%), the baseline in 214 (about 27%) (Nass et al., TOSEM, 2023).

  • Leotta et al. (2013): In an industrial case study, suites with ID locators needed less repair effort than XPath-based ones (JAMAICA paper, 2013), reported here qualitatively.

XPath vs. CSS selector: which one is faster?

There is no solid evidence that CSS selectors are noticeably faster than XPath in modern desktop browsers. Selenium says browser vendors typically don't performance-test XPath, so it can “tend to be quite slow” (Selenium, 2026). The page gives no measurements. Elemental Selenium calls most of the old speed claims outdated.

Readability is where XPath clearly loses. Selenium rates XPath equal to CSS in function, but warns that “the syntax is complicated and frequently difficult to debug.”

On iOS, the difference is measurable. The Appium XCUITest driver docs give accessibility id five stars for performance and XPath two. XPath locators are “sometimes up to 10x slower” (Appium, 2026).

When is XPath still justified?

In a locator strategy, XPath is justified when an element can only be described uniquely through its siblings or descendants, and no role, label or test ID works. Appium states the rule: “Use this strategy for elements that can only be identified when using their siblings and/or specific descendants.” (Appium, 2026).

If you use XPath, keep it relative, anchor it on text and avoid position indexes. Selenium 4 offers Relative Locators (above, below, near) as an alternative (Selenium, 2026). Typical cases for XPath:

  • Tables without test IDs, where you find the cell next to “IBAN” with the following-sibling axis.

  • XML and SOAP responses in API tests. XPath 1.0 has been a W3C Recommendation for XML documents since November 16, 1999 (W3C, 1999).

  • Legacy Selenium suites where a rewrite costs more than maintenance.

  • Third-party widgets whose markup your team can't change.

How do you agree on test IDs with developers?

A test ID contract is an agreement between development and QA that defines which elements get a data-testid attribute, how it's named and who may change it. It's the part of your locator strategy that developers own with you. The setup:

Test ID contract in 7 steps, from the attribute name to checking across browsers

Figure 4: Seven steps to a test ID contract between development and QA

  1. Pick the attribute name. By default, Playwright looks for data-testid; testIdAttribute switches it to another attribute (Playwright, 2026). Cypress uses data-cy in its examples.

  2. Limit where IDs go. Only where role or label won't do: changing text, unnamed icons, multilingual screens.

  3. Agree on a naming scheme. Area, element, purpose, for example transfer-iban-input.

  4. Add it to the Definition of Done. The test ID ships with the story that introduces the element.

  5. Treat changes as breaking changes. Anyone renaming or removing a test ID flags it in the merge request, and QA approves.

  6. Keep locators in one place. Test IDs live in page objects.

  7. Check across browsers. New locators run once through your browser matrix.

What happens when a locator breaks?

When a locator breaks, the test reports a failure the application doesn't have: the element is there, the expression just no longer matches. The fix is a manual repair in the page object or self-healing. How healing works is covered in our post on self-healing locators.

Language models help with hard cases, but they cost time and money. In a preprint by the Similo authors, GPT-4 picked among the top candidates from VON Similo. On 804 element pairs from 48 web apps, failed localizations fell from 70 to 39, “a 44 percent reduction” (Nass et al., arXiv preprint, 2023). The approach was slower and more costly, and the authors call for more research.

That points to an order: logic first, language model as fallback. Autemos is AI-assisted software for functional test automation across web, mobile, API and desktop. Autemos doesn't certify accessibility. In Autemos self-healing, its own stability logic runs first and a language model steps in only as a fallback.

Every heal is documented in the report and can be reviewed, rejected and reverted. For banks and insurers, that traceability matters: who reviewed or rejected which locator change, and when? A good locator strategy keeps heals rare, and the documentation makes the remaining ones auditable.

Frequently asked questions

What is a locator strategy?

A locator strategy is the agreed order in which a team finds elements in automated UI tests, for example role before label before test ID before CSS selector before XPath. The order decides how often tests break after a release.

Is data-testid better than getByRole?

It depends on whether the test should fail when text changes. Playwright calls test IDs the most resilient option, though not user facing, and recommends roles first. Use getByRole as the default and data-testid for changing or translated text.

Should you avoid XPath completely?

On the web, XPath should be the last option, and a full ban isn't necessary. Playwright advises against CSS and XPath. For XML responses, tables without test IDs and third-party markup, relative XPath still works. On iOS, Appium rates XPath the slowest strategy.

How do you handle multilingual interfaces?

In multilingual apps the accessible name changes per language, so role locators with a fixed name break per locale. Use test IDs, which Playwright says survive text changes, or read the expected name from the app's translation file.

Do role locators replace accessibility testing?

No. Playwright itself says role locators “do not replace accessibility audits and conformance tests”. Contrast, keyboard use and screen reader output need their own checks; see our post on accessibility testing.

Conclusion

A solid locator strategy follows a clear order: role plus accessible name, label or text for forms, test ID for changing content, CSS selector and XPath as the exception. Playwright and Testing Library put the role first, Cypress the data attribute, Selenium the HTML ID. For any single case, the Cypress question helps: should the test fail if the text changes?

In the studies, ROBULA+ cut fragility against absolute XPaths by about 90%, and Similo failed in 11% of cases instead of 27%. The UI testing guide shows where locators fit into GUI testing as a whole. Want to see documented self-healing on your own application? Talk to us.

Experience Autemos. In just 30 minutes.

See for yourself and experience how simple, flexible, and controlled modern test automation can be today.

Social Connect

© 2026 Autemos. A product of selementrix GmbH.

Experience Autemos.
In just 30 minutes.

See for yourself and experience how simple, flexible, and controlled modern test automation can be today.

Social Connect

© 2026 Autemos. A product of selementrix GmbH.

Experience Autemos.
In just 30 minutes.

See for yourself and experience how simple, flexible, and controlled modern test automation can be today.

Social Connect

© 2026 Autemos. A product of selementrix GmbH.