How a locator finds exactly one element. In this interactive walkthrough you will write locators that find exactly one element for actions, count many on purpose, and repair ambiguous or brittle ones. Simulated in your browser, free, no sign-in.
Playwright Locators
Now simulating · step 1 of 5
Look at the page
Educational locator simulator over a fixed sample page, based on the Playwright locator docs. It does not run Playwright and covers only the options shown.
One match: getByLabel('Email')
DOM excerpt
<nav aria-label="Main"> <a href="/">Home</a> <a href="/products">Products</a> <a href="/cart">Cart (0)</a> </nav> <p class="banner">Free shipping on orders over 50</p> <form> <h2>Sign in</h2> <label for="email">Email</label> <input id="email" type="email" data-testid="login-email"> <label for="password">Password</label> <input id="password" type="password" data-testid="login-password"> <button class="btn btn-primary">Sign in</button> </form> <h2>Featured products</h2> <article data-testid="product-card"> <h3>Trail Water Bottle</h3> <button id="add-1-4821" class="btn btn-primary">Add to cart</button> </article> <article data-testid="product-card"> <h3>Canvas Tote Bag</h3> <button id="add-2-4821" class="btn btn-primary">Add to cart</button> </article> <article data-testid="product-card"> <h3>Desk Plant Pot</h3> <button id="add-3-4821" class="btn btn-primary">Add to cart</button> </article> <button class="btn btn-link">Join newsletter</button>
Tip: click a coloured area or an edge line to see what it does.
Text description of this step
- Scenario: One match: getByLabel('Email').
- Locator: await page.getByLabel('Email').fill('tester@example.com');
Keyboard: Space plays or pauses (when not typing).
Learning objective: Write locators that find exactly one element for actions, count many on purpose, and repair ambiguous or brittle ones.
Step 1 · Beginner
Look at the page
The sample shop has a nav menu, a labelled login form, three product cards with the same button, and a newsletter dialog.
Expected outcome: Find the element you want to use.
Simulator, not Playwright: it models getByRole (with name), getByLabel, getByText, getByTestId, scoping inside a product card and a few CSS selectors, on this sample page only. See the official Playwright locator docs.
Many matches are not always a bug. click() and fill() need one element (strict mode); count() and toHaveCount() are meant for lists.
No universal best locator. Role and label follow what users see but change with copy; test ids survive copy changes but need upkeep; generated ids and layout CSS break easily.
About this lesson
You will learn to
- Write getByRole, getByLabel, getByText and getByTestId locators.
- Tell single-target actions from operations on many elements.
- Repair ambiguous or brittle locators and explain the trade-offs.
Before you start
- Basic HTML (elements, attributes).
- Know what Playwright is.
Your progress on this topic
- Explored
- 0 of 5 guided steps viewed
- Practiced
- Not yet: Run a locator from the builder
- Not yet: Run a locator that fails (no match, strict-mode violation or wrong element type)
- Challenges passed
- Beginner: 0 of 3 · hints 0 · wrong answers 0
- Practitioner: 0 of 3 · hints 0 · wrong answers 0
- Advanced: 0 of 2 · hints 0 · wrong answers 0
Watching the walkthrough only counts as explored. Practice needs you to use the lab yourself, and a challenge passes only when your answer meets its criteria.
Practise more
Intentional failure: Ambiguous single-target click (strict mode)
Nothing is posted anywhere. You choose where to paste the link or summary.
How to explain this in an interview
"I prefer user-facing locators: getByRole with an accessible name, then getByLabel, then getByTestId when the UI has no stable text. Actions need exactly one match, so when several elements match I scope to a container, such as the product card, instead of using first() or nth(). Generated ids and styling classes are the first things to break."
Progress is saved on this browser only. It is not linked to your account and other devices will not see it.
