Playwright Locator Chaining Not Working Fix
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,getByTestIdwhen possible - Use
filter({ hasText })andfilter({ 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
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - 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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro