Selenium locators: find elements in a way that survives change
Choose how Selenium finds an element: by id, name, CSS (Cascading Style Sheets) selector or a test attribute.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: finding a house. A house number is exact. The third house after the blue gate breaks when the gate is painted. The name plate Smith breaks when the family moves.
Tom tests Daily List, a synthetic to-do app. A tells Selenium which element to find. A , such as data-testid, can be an agreed test-facing identifier. A brittle locator depends on an incidental detail that the test does not intend to verify.
A house number is useful only if it identifies the intended house in the relevant street. Similarly, a stable-looking attribute can be duplicated, stale or on the wrong task row. Check context and match count instead of assuming that one selector is always reliable.
The picture has a limit. Visible words are sometimes the requirement being tested; a wording change can be a real defect. No locator choice guarantees that a found control is ready, correct or safe to click.
(SYNTHETIC / TRAINING ONLY: Daily List and every named person are invented.)
PLAIN WORDS · 2 in this section
- locator
- a pointer telling you where to find something
- selenium
- A free tool that drives a web browser for checks.
PICTURE IT
The picture is the one the lesson above opens with. The table below shows how each part of it matches the software.
THE REAL JOB Locate the intended UI control in its correct context and verify its identity before checking behavior.
- Inspect
- Pick
- Check
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The house number | A stable id |
| The third house after the blue gate | A position path |
| The name plate | Visible text |
| A number only the post office uses | A test attribute |
WHERE THIS PICTURE STOPS
- A stable-looking identifier can still be duplicated or attached to the wrong component.
- Text may be part of the requirement; finding an element does not prove readiness or behavioral success.
HOOK
Pick the house number.
TELL A FRIEND
A street: use the number.
OFTEN MIXED UP WITH Selenium WebDriver: your first small suite. That topic builds a first small suite. This one chooses locators that last.
Sources & standards — 2 sources
- Market practice
Locator strategies — Selenium ↗ — Selenium project · Traditional locator strategies; selecting elements in the DOM (selected actual primary passages read 7 November 2026; live Selenium guide/API documentation; no fixed edition claimed).
Selected primary passages actually read on the access date; not every linked guide. Live documentation and the explicitly tagged wait implementation have distinct version scopes. Daily List rules, people, identifiers and observations are synthetic; educational examples do not claim a vendor-package or live-app run.
- Market practice
Finding web elements — Selenium ↗ — Selenium project · First matching element in a given search context; scoping the search (selected actual primary passages read 7 November 2026; live Selenium guide/API documentation; no fixed edition claimed).
Selected primary passages actually read on the access date; not every linked guide. Live documentation and the explicitly tagged wait implementation have distinct version scopes. Daily List rules, people, identifiers and observations are synthetic; educational examples do not claim a vendor-package or live-app run.
PRACTISE AND KEEP
Sources for this topic — 2 sources
- Market practice
Locator strategies — Selenium ↗ — Selenium project · Traditional locator strategies; selecting elements in the DOM (selected actual primary passages read 7 November 2026; live Selenium guide/API documentation; no fixed edition claimed).
Selected primary passages actually read on the access date; not every linked guide. Live documentation and the explicitly tagged wait implementation have distinct version scopes. Daily List rules, people, identifiers and observations are synthetic; educational examples do not claim a vendor-package or live-app run.
- Market practice
Finding web elements — Selenium ↗ — Selenium project · First matching element in a given search context; scoping the search (selected actual primary passages read 7 November 2026; live Selenium guide/API documentation; no fixed edition claimed).
Selected primary passages actually read on the access date; not every linked guide. Live documentation and the explicitly tagged wait implementation have distinct version scopes. Daily List rules, people, identifiers and observations are synthetic; educational examples do not claim a vendor-package or live-app run.
Deepest material on this page: L4 — Standards & sources. Where a topic stops short of implementation depth, that is a deliberate coverage decision, not an oversight — see coverage.