TOPIC 08/4020 MINNOT STARTED

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.

  1. Ask for a test id
  2. Use an id or role
  3. Avoid colour and position
  4. Check what was found
Number, not colour, and check the door

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The house numberA test id or id
The door colourA colour class
Third house on the leftA position
Walking to the house to checkChecking 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.

What each part of the trick stands for
In the trickWhat it stands for
ColourA colour class
PositionA position
Test idA 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
  1. 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)

    Per the search excerpt, the traditional ways to find an element are by id, by name, by Cascading Style Sheets selector and by XPath. Ids should be unique on a page. XPath is a path through the page treated as Extensible Markup Language (XML). · Checked 2026-10-18

    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.

  2. 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)

    Describes descendant, child, next-sibling and subsequent-sibling combinators and attribute selectors with presence, exact, prefix, suffix and substring forms. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  3. 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)

    The XPath 1.0 language for addressing parts of an Extensible Markup Language (XML) document; superseded by later versions but still widely used and referenced by other World Wide Web Consortium (W3C) specifications. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-11-08

    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.

  4. 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)

    Recommends user-facing locators such as role-based ones over Cascading Style Sheets (CSS) or XPath, web-first assertions that retry, and avoiding fixed waits. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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).

  5. 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)

    Per the search excerpt, a stale element reference means the element is no longer attached to the page, usually because the page redrew it; the advice is to find the element again each time it is used. · Checked 2026-10-18

    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.

  6. 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)

    Describes intercepting a request by Uniform Resource Locator (URL) pattern and fulfilling it with fixed data, and says a matching request stalls unless continued, fulfilled or aborted. Vendor documentation. Practitioner or vendor guidance, not a standard unless stated. · Checked 2026-10-18

    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.

  7. 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.

    Ids are preferred when they are unique and consistently predictable. · Checked 2026-10-19

    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.

  8. 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)

    Course listing that summarises the topic areas: locators with XPath operators and axes, Selenium IDE, Grid and the WebDriver ecosystem, and library management for WebDriver tests. · Checked 2026-10-25

    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.