Lesson 26 — Locators: How a Test Finds Things · Practical QA
LESSON VIEW
B-04 · BASIC · PRACTICAL QA

Lesson 26 — Locators: How a Test Finds Things

Objective: The strength ladder for finding elements — and why some locators break if a designer moves a div.

⏱ ≈4 min full lesson · Converted from the B-04 module deck · English edition — Español · العربية · বাংলা in translation

⚡ Key Points

  • A locator is directions, not the thing — A locator doesn't grab an element. It's a set of directions the browser follows fresh, every time: “the button named Sen…
  • Two real locators from your suite — By role + name: “the Send button.” By stable attribute: “the link pointing at /privacy.html.” Both describe what the ele…
  • The strength ladder — Strongest first: role + name → stable attributes (href, id, name) → visible text → element type → CSS structure (div > d…
  • Why structure is the weakest rung — A locator like main > div:nth-child(3) > button breaks when ANY wrapper changes — and wrappers change constantly.
  • Plug & chug: rank them — For the Send button, rank: (a) getByRole button “Send” (b) form > div:nth-child(2) > button (c) the only button with typ…

📖 Read

In ~4 minutes: the strength ladder for finding elements — and why some locators break if a designer moves a div.

A locator is directions, not the thing: A locator doesn't grab an element. It's a set of directions the browser follows fresh, every time: “the button named Send.” If the element moves, good directions still find it. Bad directions were memorizing the furniture layout.

Two real locators from your suite: By role + name: “the Send button.” By stable attribute: “the link pointing at /privacy.html.” Both describe what the element IS, not where it happens to sit. Code: page.getByRole("button", { name: "Send" }) | page.locator('a[href="/privacy.html"]')

The strength ladder: Strongest first: role + name → stable attributes (href, id, name) → visible text → element type → CSS structure (div > div > span…). Each step down is more brittle than the one above.

Why structure is the weakest rung: A locator like main > div:nth-child(3) > button breaks when ANY wrapper changes — and wrappers change constantly. Generated suites deliberately avoid structure locators. If you see one in the wild, it's a future failure with a date.

Plug & chug: rank them: For the Send button, rank: (a) getByRole button “Send” (b) form > div:nth-child(2) > button (c) the only button with type=submit. Best → worst: a, c, b. If your ranking differs, the ladder card above is the tiebreaker.

You can rank any locator on sight. Next: Page Objects — where locators go to live.