Pull up a chair. Open any automation project that is more than a year old and look at the locators. You will usually find three kinds: a few clean ones, a lot of long strings that start from the top of the page and walk down through eight nested elements, and some that depend on position, like “the third button in the second row”. The third kind breaks first, and it breaks on a Friday evening when someone moves a banner.

So let us talk about what a locator actually is. It is a statement you make to the browser: “the thing I want can be found by this description”. Every description assumes that something about the page will not change. Your job is to choose the assumption that is least likely to be broken by people who do not know your tests exist.

Four things a locator can lean on

Think of what you could say about a Submit button on a loan application form.

You can lean on its structure: it is the last child of a div inside a form inside a section. That is what a copied absolute path gives you. Any layout change, a new wrapper for styling, an extra note above the form, and the description no longer matches.

You can lean on its appearance: it has the CSS class btn-primary-lg. Classes exist for styling, and designers rename them when the design system changes. Frameworks that generate class names automatically (many component libraries do) produce names that change on every build.

You can lean on its meaning: it is a button, and its accessible name is “Submit application”. This is how a screen reader user finds it, and it changes only when the product itself changes what the button does.

Or you can lean on an agreement: the developers add an attribute such as data-testid="submit-application" whose only purpose is to be found by tests. Nobody restyles it because nobody uses it for styling.

Meaning and agreement are the two stable ones. Structure and appearance are the fragile ones. That is the whole idea; the rest is detail.

What the tools nudge you towards

Selenium gives you the classic strategies: id, name, class name, tag name, link text, CSS selector and XPath. An id is the best of these when it is genuinely stable and unique. Many pages, however, have generated ids like input_8f3a2 that change per session, and those are worse than nothing because they look dependable.

Playwright and Testing Library push you towards the meaning route. Playwright’s recommended locators ask for an element by role and accessible name, by label text, by placeholder, or by a test id attribute (it looks for data-testid by default, and you can configure a different attribute name). A side benefit is easy to miss. If your test cannot find a button by its role and name, a screen reader user probably cannot either, so the failing locator is sometimes reporting a real accessibility defect.

XPath and CSS are not evil. They are the right tool when you need to express a relationship, for example “the Edit button in the row that contains this customer’s name”. The mistake is using them to describe position when you meant to describe a relationship. “Row 4” is position. “The row containing Kavitha Nair” is a relationship, and it survives sorting.

A small worked comparison

Suppose a travel booking site changes in one sprint. The team adds a promotional strip at the top, wraps the search form in a new container for a two-column layout, and renames btn-search to cta-search as part of a design-system upgrade. The button still says “Search flights” and still does the same thing.

  • An absolute path locator: broken by the new strip and the new container.
  • A class locator on btn-search: broken by the rename.
  • A role-plus-name locator (“button” named “Search flights”): still works.
  • A test id locator: still works, provided the developers carried the attribute across.

Now flip it. Marketing changes the button text to “Find flights” for an A/B test. The role-plus-name locator breaks, and the test id one does not. So neither choice wins everywhere. Meaning-based locators are tied to wording; agreement-based ones are tied to developer discipline. That is why many teams use roles and labels for ordinary controls and test ids for the awkward cases: icon-only buttons, repeated items, and text that marketing keeps changing.

Getting the developers on your side

A test id only works if it is written into the product code, which means asking someone to do it. Teams that treat this as a favour to QA find the attributes vanish during refactors. Teams that treat it as part of the definition of done, like adding an accessibility label, keep them. Put it in the pull request checklist, and let a failing test point at a missing attribute rather than blaming the tester.

If you cannot change the product, which is common with vendor-built portals and legacy ERP screens, fall back to the most meaningful thing available: a visible label, a stable name attribute, or an ancestor with a stable id plus a short relative path from there. Keep the path short. Each extra hop is another assumption.

Habits worth building

Keep every locator in one place, whether that is a page object, a component class or a plain module. When a redesign lands, you then edit one line instead of hunting through forty tests. Make each locator readable aloud: “the Pay now button inside the order summary” is better than a string of symbols nobody can review.

Check uniqueness on purpose. A locator that matches two elements will quietly click the first one, and your test passes while testing the wrong thing. Most modern tools can be set to fail when a locator is ambiguous; turn that on.

And read the failure message before you fix the locator. “Element not found” can mean the locator is stale, but it can also mean the page loaded late, the element sits inside a frame, or the feature really is broken. Changing the selector until the test goes green is how a suite ends up passing against a broken product.

When you next open that year-old project, do not rewrite everything. Sort the locators by how much they assume about layout, and replace the worst offenders first. A third of the effort usually removes most of the Friday-evening breakages.

Figures and names in the examples are illustrative.

Leave a Reply

Trending