Notes
The DOM
The browser reads the HTML and builds the DOM, the document object model: a tree of elements, each with a tag, attributes and text. Here is one real ShopRung product card: an article with a test id and a sku attribute, a heading with a link, a price, and an add to cart button. ShopRung builds these cards with JavaScript after the page loads, which is normal today, so the DOM keeps changing while you test.
<article class="card"
data-testid="product-card"
data-sku="MG-201">
<h2><a href="#/product/4">
Green Build Mug</a></h2>
<span class="price">$15.00
</span>
<button aria-label=
"Add Green Build Mug to cart">
Add to cart</button>
</article> Roles and names
The browser also builds an accessibility tree, which is what screen readers use. Each element has a role, like heading, link or button, and an accessible name. Usually the name is the visible text, but an aria label overrides it. So ShopRung's button shows add to cart, but its accessible name is add green build mug to cart. That gives screen reader users more context, though an accessibility checker flags it, as the accessibility episode shows. It also matters for tests, as you will see.
- article heading "Green Build Mug" link "Green Build Mug" text "$15.00" button "Add Green Build Mug to cart"
- name comes from: aria-label, then <label>, then text
- visible text ≠ accessible name
The options
There are five families of locators. Role and name: find the button named something, the way a user would. Label, placeholder or text: find the input labelled email. Test id: an attribute added for tests, like cart count. CSS selectors: tags, classes and attributes, like the element whose sku is M G two oh one. And XPath: a path language that can walk the tree, like the article containing a link with this text.
role + name
getByRole("button", {name: ...})
label / placeholder / text
getByLabel("Email")
test id
getByTestId("cart-count")
CSS selector
[data-sku="MG-201"]
XPath
//article[.//a[text()='...']] A real surprise
We ran these on ShopRung. Get by role, button, named add to cart: zero matches. The visible text says add to cart, but the accessible name is longer. Get by role with the full name, add green build mug to cart: exactly one. Get by text, add to cart: all twelve buttons. The role locator is precise, and it tests what assistive technology actually sees, so a change to the button's name breaks the test instead of passing silently.
getByRole("button",
{name: "Add to cart"}) → 0
getByRole("button", {name:
"Add Green Build Mug to cart"})
→ 1
getByText("Add to cart",
{exact: true}) → 12
the role locator shows how the
button is REALLY named Priority order
A good order of preference. First, role and accessible name, because that is how users and assistive technology find things, and it rarely changes for no reason. Second, labels, placeholders and text. Third, a test id, when there is no good role or text, such as a counter. Fourth, CSS on stable attributes. Fifth, XPath, only when you need to walk the tree. Never generated class names, long paths, or positions.
- 1 role + accessible name
- 2 label, placeholder, text
- 3 test id (data-testid)
- 4 CSS on stable attributes
- 5 XPath, only when needed
- never: generated classes (.css-1x2y3z), long paths, nth-child chains, positions
Brittle vs stable
Here are two locators that both find the green mug's button today. The brittle one is a CSS path: main, then the grid div, then the fourth article, then its button. Sort the products differently, add one, or wrap the grid in another div, and it silently finds the wrong product or nothing. The stable one says: the product card that contains green build mug, then its button. It survives layout changes because it describes meaning, not position.
brittle:
main > div.grid >
article:nth-child(4) button
breaks when: order, layout or
markup changes
stable:
getByTestId("product-card")
.filter({hasText: "Green ..."})
.getByRole("button") CSS and XPath
CSS and XPath are still worth knowing, especially with Selenium, which has no built in role locators. CSS: hash for an id, dot for a class, square brackets for attributes, greater than for a direct child, and starts with matching. XPath: attribute tests with at, exact text, an article that has a matching link inside, going up to a parent with two dots, and contains. XPath can go upwards; CSS mostly cannot.
CSS #q .price [data-sku="MG-201"] article > h2 a[href^="#/p"] XPath //button[@type='submit'] //a[text()='Green Build Mug'] //article[.//a[...]] (has) //td[.='PAID']/.. (parent) contains(@class, "out")
Strict matching
A locator that matches several elements is a bug waiting to happen. Playwright is strict: if an action's locator matches two or more elements, it fails with an error listing them, instead of clicking a random one. Selenium's find element quietly takes the first match. Either way, narrow the locator: scope it inside a parent, filter by text, or use the exact name, until it means exactly one thing.
- Playwright: actions are strict 2+ matches → error, not a random click
- Selenium findElement: takes the FIRST match silently
- narrow down: scope inside a parent .filter({ hasText }) exact names
Common mistakes
Locator mistakes. Pasting the browser's copy XPath, an absolute path from the root that breaks on any change. Generated class names from CSS frameworks. Index and nth child chains. Text that changes by language or experiment, used without thought. One test id reused for different things. And the same locator copied into many tests, so one UI change means editing dozens of files; page objects, later in this part, fix that.
- copying the browser's "Copy XPath" (/html/body/...)
- auto-generated class names
- nth-child and index chains
- text that changes by locale or A/B test, used blindly
- one test id for many things
- locators repeated in tests
Say it like this
In the interview: I prefer role and accessible name, as users and screen readers see the page, then labels and text, then data test ids, then CSS on stable attributes, and XPath last. I avoid absolute paths, generated classes and indexes. Each locator should match exactly one element, so I scope inside parents and filter by text, and I keep locators in page objects or helpers, not scattered across tests.
- "I prefer role and accessible
- name, then labels and text, then data-testid, then CSS on stable attributes, and XPath
- last. I avoid absolute paths, generated classes and indexes, make each locator match one element, scope inside parents, and keep them in page objects."