Playwright Locators and Waits: A Practical Guide

Practice Playwright locators and state-based waits on live controls. Run a complete test for enabling inputs and removing DOM elements.

Choose a locator that explains the target, then wait for the state that makes the next action valid.

A locator describes which element your test needs. A wait describes when an action or assertion can succeed. Treat these as two explicit decisions when a test fails. A longer timeout cannot repair a locator aimed at the wrong control.

Practice the state transitions

Open Dynamic Controls. The voltage input exists from the start, but it is disabled. Enable starts an asynchronous relay. Remove detaches the goat checkbox, and Add creates another element. The delay parameter sets this fixture's relay to 250 ms for repeatable practice.

Use the accessible label for the voltage input and the role plus exact name for Enable. These choices express the user-facing contract. Use the documented test id for the status message and the checkbox lifecycle. Scope locators when repeated labels appear, and resolve ambiguous matches before adding retries.

Playwright checks actionability before actions. Retrying assertions such as toBeEnabled and toHaveCount make the condition visible in the test report. Keep the locator across Remove and Add. It resolves the new checkbox when check runs.

Run the example

Use the Playwright starter with Node.js 22.12 or newer. Save the complete example as tests/guide.spec.js. Run npm ci, then npx playwright install chromium, then npm test -- tests/guide.spec.js. The starter runs desktop Chromium and a Pixel 7 Chromium profile. Each test creates a fresh browser context. Examples target the public practice site and require no account.

Verified on 4 November 2026 with Playwright 1.62.0.

import { test, expect } from '@playwright/test';

test("Stable locators and state-based waits", async ({ page }) => {
  await page.addInitScript(() => localStorage.setItem('funui-consent', 'denied'));
  await page.goto('https://funui.dev/challenges/dynamic-controls?delay=250');
  const voltage = page.getByLabel('Fence voltage (kV)', { exact: true });
  await expect(voltage).toBeDisabled();
  await page.getByRole('button', { name: 'Enable', exact: true }).click();
  await expect(voltage).toBeEnabled();
  await voltage.fill('10');
  await expect(voltage).toHaveValue('10');
  await expect(page.getByTestId('dynamic-controls-message')).toContainText('enabled');

  await page.getByRole('button', { name: 'Remove', exact: true }).click();
  await expect(page.getByTestId('dynamic-controls-goat')).toHaveCount(0);
  await page.getByRole('button', { name: 'Add', exact: true }).click();
  await page.getByTestId('dynamic-controls-goat').check();
  await expect(page.getByTestId('dynamic-controls-goat')).toBeChecked();
});

Expected result

The voltage input changes from disabled to enabled and holds 10. The status reports that it is enabled. The checkbox count reaches zero after Remove, and the newly added checkbox becomes checked. The file finishes with two passing project runs in the starter.

There is no fixed sleep in the example. Waiting for a spinner to appear can miss a short transition, while waiting for the final state directly tests the outcome. A page-wide network-idle condition also cannot prove that this input is enabled.

Diagnose a failure

If Enable matches multiple controls, inspect the scope and accessible names. If toBeEnabled times out, inspect the relay state and the trace. If check fails after Add, confirm that Add completed and the locator still describes the intended checkbox. Avoid force-clicking through a disabled or covered element unless bypassing that check is the scenario you are studying.

Continue with Dynamic Loading and the locator anti-patterns article. For the API behavior used here, see Playwright locators and auto-waiting.

Keep reading

Updated

Fun UI Playground — all challenges, bug hunts, getting started, glossary, about.