QA Graphic

XPath with Playwright page.locator

A Practical Guide

Playwright page.locator method is designed to find elements dynamically, with built-in support for multiple selector types - including CSS, text, and yes, XPath. While CSS selectors are great for straightforward queries, XPath shines when you need more flexibility or when dealing with complex DOM structures.

Here's why XPath might be your go-to:

  • Structural Navigation: XPath lets you traverse the DOM based on relationships (e.g., parent, sibling, child) rather than just classes or IDs.

  • Attribute Precision: Target elements by any attribute, not just class or id.

  • Text-Based Selection: Easily find elements containing specific text, even partial matches.

  • Dynamic Pages: XPath can handle scenarios where CSS selectors falter, like when class names are auto-generated or unpredictable.

Playwright's page.locator makes XPath a first-class citizen, so let's see it in action.

Getting Started with page.locator and XPath

The syntax for using XPath in page.locator is simple: prefix your XPath expression with xpath= or use the double-slash shorthand //. Here's the basic structure:


await page.locator('xpath=//tag[@attribute="value"]').click();

Playwright will evaluate the XPath expression and return a Locator object, which you can then interact with (e.g., click(), fill(), textContent()).

Practical Example

Let's walk through a real-world scenario where XPath and page.locator save the day.

Targeting an Element by Attribute

Imagine a login form with a button lacking a unique ID or class:


<button type="submit" data-test="login-btn">Sign In</button>

With XPath, you can target it by its data-test attribute:


const { test } = require('@playwright/test');
test('click login button', async ({ page }) => {
  await page.goto('https://example.com/login');
  await page.locator('xpath=//button[@data-test="login-btn"]').click();
});

The //button[@data-test="login-btn"] means "find any <button> element with a data-test attribute equal to login-btn."

When to Avoid XPath

While XPath is powerful, it?s not always the best choice:

  • Simple Selectors: Use CSS for #id or .class?it?s faster and more readable.
  • Dynamic IDs: If attributes change frequently, text-based or role-based selectors (role=) might be more stable.
  • Maintenance: Complex XPath expressions can become brittle if the DOM structure shifts.

 

About

Welcome to Playwright Tips and Tricks, your go-to resource for mastering the art of web automation and testing with Playwright! Whether you're a seasoned developer looking to streamline your workflows or a curious beginner eager to dive into the world of browser automation, this blog is designed with you in mind. Here, I'll share a treasure trove of practical insights, clever hacks, and step-by-step guides to help you harness the full power of Playwright - a modern, open-source tool that's revolutionizing how we interact with web applications.

Schedule

Saturday 15 Internet Tools
Sunday 16 Misc
Monday 17 Media
Tuesday 18 QA
Wednesday 19 Pytest
Thursday 20 PlayWright
Friday 21 Macintosh