Skip to content

Playwright Locator Filter Not Narrowing Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about Playwright Locator Filter Not Narrowing Fix. We cover key concepts, practical examples, and best practices.

Your locator filter doesn't narrow the selection — it still matches too many elements or matches none when it should.

The Problem

// WRONG — filter without scope
const buttons = page.locator('button');
const filtered = buttons.filter({ hasText: 'Submit' });
// This works but searches ALL buttons on the page

When there are many buttons on the page, filtering all of them is slow and may match unexpected elements.

Step-by-Step Fix

1. Filter with hasText

// RIGHT — filter by text content
import { test, expect } from '@playwright/test';

test('filter button by text', async ({ page }) => {
  await page.goto('https://example.com');

  // Only buttons containing "Submit"
  const submitButton = page.locator('button').filter({ hasText: 'Submit' });
  await submitButton.click();
});

2. Filter with has (nested locator)

// RIGHT — filter by child element presence
test('filter by child element', async ({ page }) => {
  // Find list items that contain an active indicator
  const activeItems = page.locator('li').filter({
    has: page.locator('.status-active'),
  });

  await expect(activeItems).toHaveCount(3);
});

3. Filter with hasNot

// RIGHT — exclude by text or child
test('exclude disabled buttons', async ({ page }) => {
  const enabledButtons = page.locator('button').filter({
    hasNot: page.locator('[disabled]'),
  });

  await expect(enabledButtons).toHaveCount(5);
});

4. Combine multiple filters

// RIGHT — multiple filter conditions
test('filter with multiple conditions', async ({ page }) => {
  const items = page.locator('li').filter({
    hasText: 'Active',
    hasNot: page.locator('.archived'),
  });

  await expect(items).toHaveCount(2);
  await items.first().click();
});

Expected output:

✓ filter button by text (0.6s)
✓ filter by child element (0.7s)
✓ exclude disabled buttons (0.5s)

Prevention Tips

  • Use filter({ hasText }) for text content matching
  • Use filter({ has: locator }) for child element presence
  • Use filter({ hasNot: locator }) for exclusion
  • Combine filters for precise selection
  • Prefer getByText, getByRole over generic locators with filters

Common Mistakes with locator filter

  1. Using return to exit a function early instead of wrapping a pure value in the monad
  2. Mixing let bindings with <- bindings in do notation, producing type errors
  3. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors

These mistakes appear frequently in real-world PLAYWRIGHT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### What's the difference between filter({ hasText }) and getByText?

filter({ hasText }) works on an existing locator to narrow results. getByText creates a new locator that finds elements by their text content. Use getByText for direct text queries, filter to narrow an existing locator.

Does filter support regex patterns?

Yes. filter({ hasText: /Submit|Save/i }) uses regex for flexible text matching. This is useful for case-insensitive or pattern-based filtering.

Can I chain multiple filter calls?

Yes: page.locator('li').filter({ hasText: 'Active' }).filter({ hasNot: page.locator('.hidden') }). Each filter narrows the previous result.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro