Skip to content

Playwright Locator Chaining Not Working Fix

DodaTech Updated 2026-06-24 2 min read

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

Your Playwright locator chain returns no elements or selects the wrong element — chaining doesn't narrow down the selection as expected.

The Problem

// WRONG — incorrect chaining method
test('find nested button', async ({ page }) => {
  const button = page.locator('.modal').locator('button.submit');
  // Might return buttons from outside the modal too
});

Playwright evaluates locators lazily. The chain page.locator('.modal').locator('button.submit') finds all .submit buttons inside .modal elements — but if .modal doesn't exist yet, it returns empty.

Step-by-Step Fix

1. Chain locators correctly

// RIGHT — proper locator chaining
import { test, expect } from '@playwright/test';

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

  // Chain locators to narrow scope
  const modal = page.locator('[role="dialog"]');
  const submitButton = modal.locator('button', { hasText: 'Submit' });

  await expect(submitButton).toBeVisible();
  await submitButton.click();
});

2. Use getByRole with options

// RIGHT — use accessible locators with chaining
test('find button in form', async ({ page }) => {
  const form = page.getByRole('form', { name: 'Login' });
  const submitBtn = form.getByRole('button', { name: 'Submit' });

  await expect(submitBtn).toBeEnabled();
});

3. Filter with has and hasText

// RIGHT — filter matching elements
test('find item in list', async ({ page }) => {
  const list = page.locator('ul.items');
  const item = list.locator('li').filter({ hasText: 'Active' });

  await expect(item).toHaveCount(1);  // Only "Active" items
});

4. Combine with first/last/nth

// RIGHT — positional chaining
test('select second option', async ({ page }) => {
  const options = page.locator('select option');
  await options.nth(1).click();   // Select second option (0-indexed)

  await expect(options.nth(1)).toBeChecked();
});

Expected output:

✓ find nested button (0.9s)
✓ find button in form (0.7s)
✓ find item in list (0.8s)

Prevention Tips

  • Chain locators to narrow scope: page.locator(parent).locator(child)
  • Use getByRole, getByText, getByTestId when possible
  • Use filter({ hasText }) and filter({ has: locator }) for filtering
  • Use first(), last(), nth() for positional selection
  • Locators are re-evaluated on each action — no need to re-query

Common Mistakes with locator chain

  1. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  2. Using return to exit a function early instead of wrapping a pure value in the monad
  3. Mixing let bindings with <- bindings in do notation, producing type 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 locator chaining and filter?

.locator('div').locator('button') finds buttons inside divs (hierarchy-based). .locator('button').filter({ hasText: 'Submit' }) narrows by properties. Use chaining for structure, filtering for attributes.

How do I wait for a chained locator to appear?

Chained locators auto-wait for elements to appear. Just use await locator.click() or await expect(locator).toBeVisible(). Playwright retries until the element is found.

Why does my chained locator return elements from outside the parent?

Check that the parent locator is specific enough. If .modal matches multiple elements, the chain searches inside all of them. Use first() or make the parent locator more precise.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro