TOPIC 07/6018 MINNOT STARTED

Finding elements: getByRole, getByLabel, getByText and test ids

Ask for an element by its job, its label or its words, as a person would, and keep test ids for when nothing else fits.

L1–L4 are part of the Pro pass

L0 Explain simply

Analogy: finding a person in a crowd by what they do, not by a red coat. A coat can come off. The person at the ticket desk is still the person at the ticket desk. You ask for the job, and the meeting still happens after a wardrobe change.

Daily List is a to-do list app. Tom, the tester, must press Add task from a test. He could describe the button as the third blue shape in the corner. Leo, the developer, will recolour it next week. Tom could also ask for the button named Add task.

What it is: Playwright is a library that drives a real browser. This slice is how a test finds an element. A asks for the element's job and its , the words a person and a screen reader use. The nearest neighbour is a Cascading Style Sheets (CSS) selector or an XPath, which follow the page's structure and break when that structure moves. Selenium and Cypress can use those selectors too.

Why it matters: A locator tied to colour or position fails when Leo restyles the page. A role and a name still find Add task. The test keeps checking the journey Lily cares about, instead of the markup Leo happened to write.

A person can say her job out loud. A page must say it in the markup. If the button is only a clickable box with no role, the role locator cannot see it, and that is a fault worth reporting.

(SYNTHETIC / TRAINING ONLY: Daily List and every person named here are made up.)

PLAIN WORDS · 8 in this section
recolour
change the colour of something
playwright
a free Microsoft tool that drives a browser for tests
locator
a pointer telling you where to find something
xpath
a language for picking page elements by following paths
selenium
A free tool that drives a web browser for checks.
cypress
A free tool that drives a web browser for checks.
restyles
Changes how a page looks without changing what it does.
clickable
able to be clicked

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 Finding the right element on a page by asking for its job, its label or its words.

  1. Role
  2. Label
  3. Text
  4. Test id
Ask for the job

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
Finding a person by their jobFinding an element by its role and name
Their name badgeThe accessible name
A sign that only the organisers readThe test id attribute
Matching any badge that contains your wordThe substring match

WHERE THIS PICTURE STOPS

  • A person says her job aloud. A page must declare it in its code.
  • A crowd is real, but a page can be rebuilt between two runs.
  • A job can be shared by many people, and many elements can share a role and name.

HOOK

Ask for the job, not the coat.

REMEMBER IT THIS WAY

A contrast: two neighbours told apart by one test.

Say what it does, "the button called Add task", not how it looks, "the third blue thing in the corner".

What each part of the trick stands for
In the trickWhat it stands for
What it doesthe button called Add task
How it looksthe third blue thing in the corner

TELL A FRIEND

Finding a friend at a station by her job and not her coat.

OFTEN MIXED UP WITH Locators: finding an element with CSS selectors and XPath. The earlier lesson ranks ids, Cascading Style Sheets (CSS) and XPath. This lesson adds the built-in locators that read the page like a person.

Sources & standards — 4 sources
  1. Market practice

    Locators (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.

    Says role locators are the preferred choice because they match how users and assistive tools see the page, label locators suit form fields, text locators suit non-interactive elements, and test ids are the most resilient fallback when role or text cannot be used. Says the test id attribute is data-testid by default and can be changed with testIdAttribute. Says a matching locator that resolves to more than one element throws in strict mode for actions, and that first, last and nth are not recommended. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.

  2. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators such as role-based ones over CSS or XPath, because the page structure can change (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).

  3. Market practice

    Accessible Rich Internet Applications (ARIA) 1.2 ↗ — World Wide Web Consortium (W3C) · Roles are defined in a standard, and some roles need an accessible name (search excerpt, page not opened)

    Roles, states and properties, and which roles need an accessible name. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: World Wide Web Consortium (W3C) Recommendation, 2023. 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.

  4. Market practice

    Using Accessible Rich Internet Applications (ARIA) (W3C Working Group Note) ↗ — World Wide Web Consortium (W3C) · Use a native HTML element when one gives the meaning and behaviour needed (search excerpt, page not opened)

    The first rule of Accessible Rich Internet Applications (ARIA): use a native HyperText Markup Language (HTML) element when one gives the meaning and behaviour needed. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: World Wide Web Consortium (W3C) Working Group Note. 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.

PRACTISE AND KEEP

Sources for this topic — 4 sources
  1. Market practice

    Locators (Playwright documentation) ↗ — Microsoft (Playwright) · Reviewed 6 November 2026: Complete extracted primary article; examples inspected, not executed. See v45 source analysis for version scope and documented source ambiguities; no upstream run claimed.

    Says role locators are the preferred choice because they match how users and assistive tools see the page, label locators suit form fields, text locators suit non-interactive elements, and test ids are the most resilient fallback when role or text cannot be used. Says the test id attribute is data-testid by default and can be changed with testIdAttribute. Says a matching locator that resolves to more than one element throws in strict mode for actions, and that first, last and nth are not recommended. Vendor documentation for one tool. Practitioner guidance, not a standard. · Checked 2026-11-06

    Primary source opened and operative text inspected on 6 November 2026. Scope: Complete extracted primary article; examples inspected, not executed. Current live Playwright documentation reflects 1.65 where stated; explicit older examples retain their version. Source inspection is not an execution or compatibility-test claim. See the completed v45 Playwright analysis for source discrepancies and exact reading boundaries.

  2. Market practice

    Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer user-facing locators such as role-based ones over CSS or XPath, because the page structure can change (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).

  3. Market practice

    Accessible Rich Internet Applications (ARIA) 1.2 ↗ — World Wide Web Consortium (W3C) · Roles are defined in a standard, and some roles need an accessible name (search excerpt, page not opened)

    Roles, states and properties, and which roles need an accessible name. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: World Wide Web Consortium (W3C) Recommendation, 2023. 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.

  4. Market practice

    Using Accessible Rich Internet Applications (ARIA) (W3C Working Group Note) ↗ — World Wide Web Consortium (W3C) · Use a native HTML element when one gives the meaning and behaviour needed (search excerpt, page not opened)

    The first rule of Accessible Rich Internet Applications (ARIA): use a native HyperText Markup Language (HTML) element when one gives the meaning and behaviour needed. · Checked 2026-10-18

    Found through a web search; the page was not opened (search excerpt, page not opened). Version: World Wide Web Consortium (W3C) Working Group Note. 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.