Playwright Python Locators and Auto-Waiting: A Beginner's Guide
In the last lesson, the test did one thing: load a page and check its title. Most tests need to interact with the page. They click a button, fill a field or read some text back. That takes two ideas working together, locators and auto-waiting, and this lesson covers them one at a time.
Before you start: you need the project from the earlier lessons, with your virtual environment active. If you haven't written a test yet, start with Your First Playwright Python Test.
What a locator is
A locator tells Playwright how to find something on the page. Think of it as giving directions. You write them once, for example "the button with this text", and Playwright follows them fresh every time it needs to act on that element.
Step 1: Write a test that clicks a button
In tests/concepts, create a new file called test_locators.py. The test opens the demo shop and clicks an Add To Cart button:
def test_add_to_cart_button_click(page):
page.goto("https://qa-cart.com/demoshop/")
page.get_by_role("button", name="Add To Cart").click()As in Lesson 2, the function name starts with test and takes the page fixture. The new part is the last line.
Step 2: How get_by_role works
get_by_role is one of Playwright's built-in locators. Every element on a web page has a role, such as button, textbox or link. Roles come from accessibility standards.
So role="button" with name="Add To Cart" describes the element the way a user would: the button that says Add To Cart. It doesn't depend on CSS classes or the page structure.
You can see an element's role yourself. Open your browser's Inspect tool, go to the Accessibility panel and highlight the element. For the Add To Cart button, the role is button.
.click() then tells Playwright to click that element. get_by_role is one of several built-in locators. The Playwright locators guide lists the rest.
Step 3: Auto-waiting, a separate job
Before Playwright acts on an element, whether that's clicking a button or typing into a field, it automatically waits for the element to be ready. For a click, it runs actionability checks and makes sure the element is:
Visible
Stable, not still moving into place
Able to receive events, with nothing else covering it
Enabled
Playwright won't click until all of these are true. If the page is still loading or something is still moving, it waits on its own, so you never write a manual wait or sleep before the click. The auto-waiting page in the Playwright docs lists the checks for each action.
The locator says which element. Auto-waiting says don't act until it's ready. They are two different jobs.
Step 4: Run it and watch it
Playwright runs headless by default, so the browser works in the background. While you're learning, it helps to see what's happening. Run the test in headed mode, with a short delay between actions:
pytest tests/concepts/test_locators.py --headed --slowmo 500--headed shows the browser, and --slowmo 500 adds a 500 millisecond pause before each action so you can follow along.
Step 5: Read the strict mode violation
The test fails. Not because the locator is wrong, but because it's too broad. The demo shop has several Add To Cart buttons, one per product.
Playwright expects a locator to match exactly one element before it acts. Instead of guessing and clicking whichever button it finds first, it stops with a strict mode violation: more than one element matches.
For now, make it specific by taking the first match with .first:
def test_add_to_cart_button_click(page):
page.goto("https://qa-cart.com/demoshop/")
page.get_by_role("button", name="Add To Cart").first.click().first means: of all the matching buttons, use the first one. Save and run again, and the test passes.
The key idea: a locator should ideally match one element. If it matches several, Playwright stops you rather than guessing. .first is fine for learning, and there are more precise ways to narrow a locator later in the course. There's also a short video on strict mode violations if you want to see it again.
What you covered
What a locator is: directions Playwright follows every time it acts
get_by_role finds elements by role and name, the way a user sees them
Auto-waiting: Playwright checks an element is visible, stable, receiving events and enabled before acting
Running headed with --slowmo to watch a test
Strict mode: a locator should match one element, and .first narrows it for now
The test clicks something but doesn't check anything yet. Next you add a search test and verify what shows on the page with expect assertions.
The code for the whole course is in the playwright-python-course-yt repo on GitHub.
Get Started with Playwright Python: the free series
This post is Lesson 3 of my free Playwright Python course on YouTube.
Watch this lesson: Lesson 3 on YouTube
Next lesson: Lesson 4, expect Assertions with a Search Test
Watch the full series in order: Playwright with Python playlist
Course page, lessons and code: anuradhaagarwal.com/playwright-python



