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.
- Role
- Label
- Text
- Test id
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| Finding a person by their job | Finding an element by its role and name |
| Their name badge | The accessible name |
| A sign that only the organisers read | The test id attribute |
| Matching any badge that contains your word | The 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".
| In the trick | What it stands for |
|---|---|
| What it does | the button called Add task |
| How it looks | the 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
- 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.
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.
- 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)
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
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)
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.
- 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)
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
- 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.
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.
- 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)
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
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)
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.
- 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)
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.