Locators: finding an element with CSS selectors and XPath
Pick a handle for each element that survives a redesign, write it as a Cascading Style Sheets (CSS) selector or an XPath, and fix stale-element and wrong-element slips.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: directions to a house. Say "the house with the blue door" and a visitor finds it, until the owner paints the door. Say "number 12" and the visitor still finds it after the paint. Say "third house on the left" and the visitor gets lost when a new house is built.
Daily List is a to-do list app. Tom's check must find the Add task button on the page. The way it finds the button is called a locator.
After a redesign, a locator built on the colour of the button stopped working. A locator built on a test id, a label the team puts on the button for tests, still worked.
Worse, a locator built on position found the wrong button, and the check passed. Tom could not see it.
So a good locator is the number on the door. It belongs to the element. It does not change when the page is restyled.
The picture has limits. A house number is fixed by the council, but in an app someone must agree to keep a test id. Ask the developers to add one.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named in this lesson are invented.)
PLAIN WORDS · 2 in this section
- locator
- a pointer telling you where to find something
- restyled
- Given a new look, for example new colours.
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 Picking a handle for an element that stays the same when the page is restyled, and checking what was found.
- Ask for a test id
- Use an id or role
- Avoid colour and position
- Check what was found
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The house number | A test id or id |
| The door colour | A colour class |
| Third house on the left | A position |
| Walking to the house to check | Checking what the locator found |
WHERE THIS PICTURE STOPS
- A house number is set by the council. A test id needs a developer to agree.
- A visitor can ask a neighbour. A check cannot.
- Houses rarely move. Rows in a list often do.
HOOK
Number, not colour; check the door.
REMEMBER IT THIS WAY
A story beat: one picture-line that carries the order or the cause.
After the redesign, the colour locator broke. The position locator quietly clicked the wrong button and the check still passed.
| In the trick | What it stands for |
|---|---|
| Colour | A colour class |
| Position | A position |
| Test id | A test id or id |
It misleads when the page has no test id, so ask a developer to add one.
TELL A FRIEND
Directions to a house use the number, not the door colour. A locator uses the test id, not the button colour.
OFTEN MIXED UP WITH How UI automation works: browsers, drivers and structure. The concepts lesson shows how reaches the page. This lesson shows how it finds one element there.
PRACTISE AND KEEP
Sources for this topic — 8 sources
- Market practice
Locator strategies (Selenium documentation) ↗ — Selenium project · Selenium lists locator strategies such as id, name, class name, CSS selector and XPath (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Cascading Style Sheets (CSS) selectors and combinators (Mozilla developer docs) ↗ — Mozilla · Selectors and combinators: descendant, child, next-sibling and subsequent-sibling; attribute selectors with presence, exact, prefix, suffix and substring forms (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
XPath Version 1.0, World Wide Web Consortium Recommendation of 16 November 1999 ↗ — World Wide Web Consortium · XPath 1.0 addresses parts of an XML document; superseded by later versions but still widely used (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Recommendation, 16 November 1999. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
8 November2026 bounded primary update: XPath1.0 W3CRecommendation16November1999 selected locationpaths/steps and expanded-name element paragraphs actually read8November2026. Namespace-aware selectors; not DukeXMLvalidity evidence or wholelanguage audit.
- Market practice
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators such as role-based ones over CSS or XPath, which can break when the page structure changes (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail. A second search on 2026-10-20 returned this page for its title and matched the described content; the page was still not opened (search excerpt, page not opened).
- Market practice
Understanding common errors (Selenium documentation) ↗ — Selenium project · A stale element reference means the element was located earlier and is no longer attached to the page; locate it again (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Mock APIs (Playwright documentation) ↗ — Microsoft (Playwright) · Intercept a request by URL pattern and fulfil it with fixed data (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Version: Web page, no edition number. Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
Tips on working with locators ↗ — Selenium project · r8a-selenium-locator-tips (ids preferred when unique and consistently predictable). Selenium has no page that says avoid dynamic ids outright; the first three sentences are teaching inference. Search excerpt, page not opened.
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
- Market practice
A4Q Certified Selenium Tester Foundation Level (iSQI course listing) ↗ — iSQI · Course listing names XPath operators, axes, relative and absolute paths (search excerpt, page not opened)
Found through a web search; the page was not opened (search excerpt, page not opened). Licence terms were not read, so put it in your own words and do not quote it. Open the page and confirm what it says before relying on any detail.
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.