Playwright Locators with Python
Locators are one of the most important concepts in browser automation. They help automation scripts identify and interact with elements present on a web page such as buttons, text fields, links, and dropdowns.
In this tutorial, we will learn what locators are in Playwright and explore different locator strategies available in Playwright with Python.
What are Locators in Playwright?
A locator is a way to identify and find elements on a web page. Automation scripts use locators to perform actions like clicking a button, entering text into an input field, or validating content on the page.
Playwright provides powerful locator strategies that make element identification more reliable and easier compared to traditional automation frameworks.
Example of locating and clicking a button:
page.locator("button").click()
Types of Locators in Playwright
Playwright supports multiple locator strategies to identify web elements.
- Text Locator
- CSS Locator
- XPath Locator
- Role Locator
- Test ID Locator
Using the correct locator strategy improves test stability and reliability.
Text Locator
Text locators allow you to locate elements based on visible text present on the webpage.
Example: Clicking a button using text.
page.get_by_text("Login").click()
This command finds an element containing the text "Login" and clicks it.
Text locators are very useful when buttons or links contain clear text labels.
CSS Locator
CSS selectors are commonly used to locate elements using their HTML attributes such as id, class, or tag name.
Example: Locating an input field using its ID.
page.locator("#username").fill("admin")
Example: Locating an element using class name.
page.locator(".login-button").click()
CSS locators are fast and widely used in automation frameworks.
XPath Locator
XPath locators allow you to locate elements based on their position in the HTML structure.
Example of locating an element using XPath.
page.locator("//input[@name='username']").fill("admin")
Although XPath is powerful, it is recommended to use it only when CSS selectors or other locators cannot identify the element.
Role Locator
Role locators are one of the most powerful features of Playwright. They allow elements to be identified based on their accessibility role.
Example: Clicking a button using role locator.
page.get_by_role("button", name="Login").click()
Role locators make automation tests more reliable and aligned with web accessibility standards.
Chaining Locators
Sometimes elements are located inside other elements. In such cases, Playwright allows chaining locators to narrow down the search.
Example:
page.locator("#login-form").locator("button").click()
Here Playwright first finds the login form and then locates the button inside it.
Chaining locators improves element accuracy and avoids incorrect element selection.
Handling Dynamic Elements
Modern web applications often generate dynamic elements where IDs or attributes change each time the page loads.
Playwright provides flexible locators that can handle such scenarios.
Example using partial text matching:
page.get_by_text("Submit").click()
Example using CSS partial attribute matching:
page.locator("input[name*='user']").fill("admin")
Using stable attributes and meaningful locators helps create robust and maintainable automation scripts.
Playwright Locator Quick Reference Table
The following table provides a quick summary of commonly used Playwright locators. This can be used as a quick reference while writing automation scripts.
| Locator Method | Description | Example |
|---|---|---|
| get_by_text() | Finds an element using visible text on the page | page.get_by_text("Login").click() |
| get_by_role() | Finds elements using accessibility roles such as button, link, textbox | page.get_by_role("button", name="Submit").click() |
| locator() | Finds elements using CSS selectors | page.locator("#username").fill("admin") |
| xpath | Finds elements using XPath expressions | page.locator("//input[@name='username']") |
| get_by_label() | Finds form elements using their associated label text | page.get_by_label("Email").fill("test@email.com") |
| get_by_placeholder() | Finds input elements using placeholder text | page.get_by_placeholder("Enter username").fill("admin") |
| get_by_title() | Finds elements using the title attribute | page.get_by_title("Search").click() |
| get_by_test_id() | Finds elements using custom test-id attributes | page.get_by_test_id("login-button").click() |
| button | Locates button elements using CSS selector | page.locator("button").click() |
| input | Locates input fields | page.locator("input").fill("text") |
Using these locator strategies effectively helps create stable and reliable automation scripts in Playwright.
Conclusion
Locators are the foundation of any automation framework. Understanding how to identify elements using different locator strategies helps create stable and reliable automation tests.
Playwright provides powerful locator options such as text, role, CSS, and XPath which make element identification easier and more efficient.
In the next tutorial, we will learn how to perform web element actions such as click, type, keyboard actions, and mouse interactions using Playwright with Python.
No comments:
Post a Comment