Playwright + Python · Lesson 16 of 37

0/37 done

Module 16: Locators

16. User-Facing Locators & Resilient Query Strategies

18 minlesson

Your task

Implement get_button_by_name(page, btn_name):

  1. Return page.get_by_role("button", name=btn_name)

Write it in the editor, then press Check answer (Ctrl + Enter).

Accessible Locators Hierarchy

  1. page.get_by_role("button", name="Submit")
  2. page.get_by_label("Email Address")
  3. page.get_by_placeholder("Enter password")
  4. page.get_by_text("Welcome back")
  5. page.get_by_test_id("checkout-btn")
python
row = page.locator("tr").filter(has_text="ORD-9421")
row.get_by_role("button", name="Cancel").click()

Key Takeaways & Walkthrough Summary

Playwright locators represent a query that is evaluated right when an action occurs. In this lesson, you will master accessible user-facing locators like get_by_role, get_by_label, get_by_placeholder, and locator filtering. Let's master locators!

Part of the Playwright + Python course (Playwright with Python). Practice this concept live in the Playwright Online Compiler or explore our free automation practice form.

Common Doubts & Lesson Q&A

What is strict mode in Playwright?

If a locator resolves to more than one matching element, Playwright raises a strict mode violation error, highlighting UI ambiguities.

Ready? Solve the task in the editor

Use Check answer to mark it complete. Hints are there if you need them.

Python
Skip