Narrowing locators: chaining, filtering, lists and nth
When a locator matches many rows, search inside one row instead of counting positions, and let strict mode warn you when you were not exact.
L1–L4 are part of the Pro pass
L0 Explain simply
Analogy: a letter addressed to the kitchen at number 12, not to every kitchen on the street. Every house has a kitchen. The street name, the house number, and the room make one kitchen. Each step makes the search smaller.
Daily List is a to-do list app. The Today list shows many tasks, and every row has a Delete button. Tom, the tester, wants to delete only Pay rent. The words Delete match every row.
What it is: Playwright is a library that drives a real browser. This slice is narrowing a locator that matches too many elements. searches inside one match. A keeps only the matches that fit a clue. The nearest neighbour is counting with nth, which picks a position and breaks when the list shifts. Selenium and Cypress also make you choose one match before a click.
Why it matters: A vague click can delete the wrong task and still look like a pass if the test only counts rows. Narrowing to the row named Pay rent makes the test say which task it means.
A street does not change while you walk it. A page can change between steps, so Playwright looks again each time it acts.
(SYNTHETIC / TRAINING ONLY: Daily List and every person named here are made up.)
PLAIN WORDS · 4 in this section
- playwright
- a free Microsoft tool that drives a browser for tests
- locator
- a pointer telling you where to find something
- selenium
- A free tool that drives a web browser for checks.
- cypress
- 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 Narrowing a search that matches many things down to the one thing you mean.
- Find the list
- Filter to the row
- Chain to the control
- Act once
HOW THE PICTURE MAPS
| In the picture | In the software |
|---|---|
| The street | The list or the page |
| The house at one number | The row that matches a clue |
| The kitchen inside that house | The button inside that row |
| A letter that says only the kitchen | A vague locator that strict mode refuses |
WHERE THIS PICTURE STOPS
- A street does not change while you search it. A page can change between steps.
- A house number is fixed. A row clue such as a task title can appear twice.
- The postman asks you to be exact. Strict mode only warns, it does not fix the clue.
HOOK
Street, house, room.
REMEMBER IT THIS WAY
A rule of thumb: if you see this, think that.
If a locator matches many rows, find the row by its name first, then look inside that row only.
| In the trick | What it stands for |
|---|---|
| Row by its name | the row called Pay rent |
| Inside that row | the Delete button inside that row only |
If two rows share the same name, the name alone still matches both.
TELL A FRIEND
Searching a street, then a house, then a room, until one kitchen is left.
OFTEN MIXED UP WITH Finding elements: getByRole, getByLabel, getByText and test ids. The earlier lesson finds one element by its job. This lesson narrows a locator that matches many elements.
Sources & standards — 3 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
Locator class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Locator filter takes hasText, hasNotText, has and hasNot; a string matches as a case-insensitive substring; the inner locator of has is matched from the outer one; and() and or() combine locators; count does not throw (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
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer locators that point at one element in a way that survives page 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).
PRACTISE AND KEEP
Sources for this topic — 3 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
Locator class reference (Playwright documentation) ↗ — Microsoft (Playwright) · Locator filter takes hasText, hasNotText, has and hasNot; a string matches as a case-insensitive substring; the inner locator of has is matched from the outer one; and() and or() combine locators; count does not throw (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
Best practices (Playwright documentation) ↗ — Microsoft (Playwright) · Prefer locators that point at one element in a way that survives page 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).
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.