TOPIC 08/6018 MINNOT STARTED

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.

  1. Find the list
  2. Filter to the row
  3. Chain to the control
  4. Act once
Street, house, room

HOW THE PICTURE MAPS

How each part of the picture matches the software
In the pictureIn the software
The streetThe list or the page
The house at one numberThe row that matches a clue
The kitchen inside that houseThe button inside that row
A letter that says only the kitchenA 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.

What each part of the trick stands for
In the trickWhat it stands for
Row by its namethe row called Pay rent
Inside that rowthe 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
  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

    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)

    Reference for the Locator class: filter with hasText, hasNotText, has and hasNot (text matching is case-insensitive substring for a string), and, or, locator, nth, first, last, count and all. Says the inner locator of has is matched starting from the outer one. Says hasNot and hasNotText arrived in version 1.33 and and() in version 1.34. Vendor documentation for one tool. Practitioner guidance, not a standard. · 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

    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)

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

PRACTISE AND KEEP

Sources for this topic — 3 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

    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)

    Reference for the Locator class: filter with hasText, hasNotText, has and hasNot (text matching is case-insensitive substring for a string), and, or, locator, nth, first, last, count and all. Says the inner locator of has is matched starting from the outer one. Says hasNot and hasNotText arrived in version 1.33 and and() in version 1.34. Vendor documentation for one tool. Practitioner guidance, not a standard. · 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

    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)

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

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.