One line of code puts the right product in the cart on one page and the wrong product on another, and the test stays green on both. This lesson shows how that happens and gives you one question to ask of every locator: if the list is reordered, does this line still find the same thing? It follows the official Locators docs, and every command and output below comes from a real run.
What you need
- Node.js. 22, 24, 26.
- Playwright 1.65.1. The lesson was recorded with this version; other versions may print slightly different output.
- The code. Subscribers see the lesson repository in the Source code box under the video, with the tags
pw03-startandpw03-end.
From this lesson on, the demos run against automationexercise.com, a practice shop. The site changes, and it sometimes shows a "One moment, please..." page to automated browsers. If a number is 0 or a test times out for no reason, run it again a little later. The numbers below are examples; yours may differ.
Step 1: One line, two pages, two products
tests/nth.spec.ts clicks the ninth "Add to cart" on /products and on /products?search=top:
await page.getByText('Add to cart').nth(8).click();
Both tests pass. tests/nth-cart.spec.ts goes one step further and opens the cart to read the product name:
npx playwright test tests/nth-cart.spec.ts --workers=1
On /products the cart holds Winter Top. On the search page it holds Fancy Green Top, so the check fails. The line did exactly what it said, "the ninth button", and the ninth button is a different product on each page. tests/cart.spec.ts shows the same thing as a single error: Expected: "Winter Top", Received: "Fancy Green Top".
Fails on purpose.
nth-cart.spec.tsandcart.spec.tsare red by design.
Step 2: How many things does the text match?
npx playwright test tests/count.spec.ts --workers=1
tests/count.spec.ts prints how many elements getByText('Add to cart') matches on three pages. In the video: 68 on /products, 28 on the search page and 6 on /category_products/1. Every product appears twice, because two elements share the same data-product-id, so each number is double the product count.
Step 3: What a locator is
The Locators page opens with a definition: a locator is a way to find elements on the page at any moment. It lists seven built-in ones:
| Locator | Finds by |
|---|---|
getByRole | role and accessible name (recommended first) |
getByText, getByLabel, getByPlaceholder | visible text, label, placeholder |
getByAltText, getByTitle | image alt text, title attribute |
getByTestId | a test id attribute |
The docs advise prioritizing what users see and hear, not the structure of the page.
Step 4: Cause 1, the text matches too much
npx playwright test tests/strict.spec.ts --workers=1
tests/strict.spec.ts clicks getByText('Add to cart') with nothing to narrow it. Both tests fail with strict mode violation: getByText('Add to cart') resolved to 68 elements (28 on the search page). Playwright refuses to guess which one you meant. That is a good error: red at the right time, for the right reason.
Fails on purpose.
strict.spec.tsis red by design.
Step 5: Cause 2, the role finds nothing
npx playwright test tests/role.spec.ts --workers=1
tests/role.spec.ts waits for getByRole('button', { name: 'Add to cart' }). This time it is not 68 matches but zero: TimeoutError: locator.click: Timeout 5000ms exceeded on both pages.
Look at the page the way Playwright does. In DevTools press Ctrl+Shift+C and hover the button: the tooltip says role link. Write exactly that and count:
npx playwright test tests/role-link.spec.ts --workers=1
It prints all 0 and filtered 0. DevTools says link, Playwright finds none. Ask Playwright itself:
npx playwright test tests/aria-card.spec.ts --workers=1
The card is a heading, a paragraph with the product name, then - text: Add to cart. The button is an a tag without an href, and Playwright sees only text. DevTools is a good start; Playwright has the last word.
Step 6: Cause 3, position-based CSS
tests/css.spec.ts clicks .features_items > div:nth-child(7) .productinfo a. It works on /products and puts Fancy Green Top in the cart on the search page. tests/css-6.spec.ts uses nth-child(6) and lands on Stylish Dress. tests/children.spec.ts prints the first children of .features_items: the first is the page heading and the second is the "Added!" dialog, so the first product is child 3 and every number is shifted by two.
npx playwright test tests/css.spec.ts tests/css-6.spec.ts tests/children.spec.ts --workers=1
The docs say it plainly: CSS and XPath selectors can break when the DOM structure changes.
Step 7: The fix, call it by name
tests/filter.spec.ts finds the product card that contains the text Winter Top, then the button inside it:
page.locator('.productinfo').filter({ hasText: 'Winter Top' }).getByText('Add to cart')
npx playwright test tests/filter.spec.ts tests/cart-filter.spec.ts --workers=1
All pass on both pages, and the cart holds Winter Top. If the list is reordered, the line still clicks the right product. The video puts the same task through each locator:
| Locator | /products | ?search=top |
|---|---|---|
getByText('Add to cart') | strict mode, 68 | strict mode, 28 |
getByRole('button', ...) | not found | not found |
CSS div:nth-child(7) | Winter Top | Fancy Green Top |
getByText(...).nth(8) | Winter Top | Fancy Green Top |
.filter({ hasText: 'Winter Top' }) | Winter Top | Winter Top |
Only the locator that uses the name is right on both pages.
Step 8: Seven ways on the login page
tests/testid.spec.ts uses getByTestId('login-email'). The site's data-qa attributes play the role of test ids, so the config sets testIdAttribute: 'data-qa'. Then count what each built-in locator matches on /login:
npx playwright test tests/locator-counts.spec.ts --workers=1
npx playwright test tests/login-detail.spec.ts --workers=1
| Locator | Matches on /login |
|---|---|
| role | 1 |
| text | 1 |
| label | 0 (the page has no label tags) |
| placeholder | 3 (two form inputs and the footer box) |
| alt text | 1 |
| title | 0 |
| test id | 1 |
No method is always right: count before you trust one.
Step 9: Tools that suggest locators
Two tools suggest locators. Codegen writes the test while you click around, and UI mode lets you pick an element from a snapshot:
npx playwright codegen https://automationexercise.com/products?search=top
npx playwright test --ui
| Tool | What it gave |
|---|---|
| Codegen | page.getByText('Add to cart').nth(3).click(): it also counts by position when the page gives no unique name |
| Pick locator in UI mode | open an action's snapshot, click Pick locator, hover the button: the Locator box showed getByText('Add to cart').nth(3) and the Aria snapshot box showed - text: Add to cart |
The tools help you find a locator fast. What to call the element by is still your decision.
Tests that fail on purpose
| File | Fails because |
|---|---|
strict.spec.ts | the text matches 68 (or 28) elements |
role.spec.ts | no element has the role button |
cart.spec.ts, nth-cart.spec.ts | nth(8) picks another product on the search page |
css.spec.ts, css-6.spec.ts | nth-child picks another product |
The files that print numbers (count, children, aria-card, role-link, locator-counts, login-detail) pass and only log.
Check your result
npx playwright test tests/count.spec.ts --workers=1→ prints three numbers, the first two are 68 and 28npx playwright test tests/filter.spec.ts tests/cart-filter.spec.ts --workers=1→ passed
Recap
| Locator | Breaks when |
|---|---|
nth(8), nth-child(7) | the list is reordered or something is added |
getByText('Add to cart') | the text appears more than once |
getByRole('button', ...) | the element is not a button for Playwright |
filter({ hasText }), getByTestId | the name or the test id changes |
- A locator that counts position clicks the wrong thing without any error.
- Count what a locator matches before you trust it.
- Ask Playwright (aria snapshot), not only DevTools, what an element is.
- Call the element by the name a user would use.
Next lesson
Next, debugging: what to open first when a test fails.