Advanced Selectors: nth, has, and filter in Playwright
Playwright is a powerful end-to-end testing framework that offers rich selector APIs for targeting elements in modern web applications. While basic selectors like text=, css=, or id= are sufficient in many cases, complex DOM structures and dynamic content often require more advanced approaches. That’s where Playwright’s advanced selectors — such as nth, has, and filter — come into play.
In this blog, we’ll explore how these selectors work, when to use them, and best practices to make your Playwright tests more reliable and efficient.
1. The nth Selector
The nth selector is used to target an element by its index among a list of matching elements. This is particularly useful when multiple elements match a selector, and you need to interact with a specific one.
Syntax:
js
page.locator('div.item').nth(2);
Example:
Let’s say you have a list of products on a page:
html
<div class="product">Product A</div>
<div class="product">Product B</div>
<div class="product">Product C</div>
To click on the second product:
js
await page.locator('.product').nth(1).click(); // Index starts at 0
Use Case:
- Selecting the nth row in a table.
- Clicking the third button in a list of action buttons.
- Verifying content of a particular list item.
2. The has Selector
The has selector allows you to match a parent element that contains a child element or text. It’s extremely helpful when multiple similar containers are present, but you want the one that contains specific sub-elements.
Syntax:
js
page.locator('div.card', { has: page.locator('button:has-text("Submit")') });
Example:
Given this HTML:
html
Copy
Edit
<div class="card">
<p>Card 1</p>
<button>Submit</button>
</div>
<div class="card">
<p>Card 2</p>
</div>
To click the button inside the card that has the "Submit" button:
js
Copy
Edit
await page.locator('.card', { has: page.locator('button:has-text("Submit")') }).locator('button').click();
Use Case:
Targeting forms that contain specific fields.
Finding containers with certain child elements (e.g., images, icons).
Interacting with rows in a grid that contain specific data.
3. The filter Method
The filter() method allows further narrowing of a locator based on attributes or text content. It’s especially useful when the locator matches multiple elements but only some meet additional criteria.
Syntax:
js
page.locator('div.product').filter({ hasText: 'Laptop' });
Example:
Given:
html
<div class="product">Laptop - $1000</div>
<div class="product">Phone - $500</div>
To click the product that mentions "Laptop":
js
Copy
Edit
await page.locator('.product').filter({ hasText: 'Laptop' }).click();
Use Case:
Selecting elements based on dynamic text.
Clicking buttons or links with unique inner content.
Validating presence of specific product names or labels.
Best Practices
Combine nth, has, and filter for maximum precision.
Avoid over-relying on hardcoded indexes (nth) if the UI changes frequently.
Use has and filter for more semantic and stable test locators.
Always prefer role, aria-label, and data-testid attributes for accessibility and testability.
Conclusion
Advanced selectors like nth, has, and filter significantly enhance the flexibility and reliability of your Playwright test scripts. They help you write cleaner, more readable, and more robust tests — especially when dealing with dynamic content or complex DOM hierarchies. Mastering these selectors will elevate your test automation and make maintaining your test suite easier as your application evolves.
Learn Playwright Testing Training
Read More: Building an Internal Playwright Testing CLI
Visit IHUB Talent Institute Hyderabad
Get Direction
Comments
Post a Comment