Skip to content

Playwright Element Wait Not Working Fix

DodaTech Updated 2026-06-24 2 min read

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

Your test times out waiting for an element that should be there — or proceeds before the element is ready.

The Problem

// WRONG — element not yet rendered
test('find dynamic element', async ({ page }) => {
  await page.goto('https://example.com');
  await page.locator('.late-loading-content').click();
  // Element doesn't exist yet — throws timeout
});
TimeoutError: locator.click: Timeout 5000ms exceeded.
  waiting for locator('.late-loading-content')

The element doesn't exist in the DOM when the locator tries to find it. It loads after an API call or timeout.

Step-by-Step Fix

1. Use locator.state wait

// RIGHT — wait for specific states
import { test, expect } from '@playwright/test';

test('wait for visible element', async ({ page }) => {
  await page.goto('https://example.com');

  await page.locator('.late-loading-content').waitFor({
    state: 'visible',
    timeout: 10000,
  });

  await page.locator('.late-loading-content').click();
});

2. Use auto-waiting locator methods

// RIGHT — Playwright auto-waits for actions
test('auto-wait for click', async ({ page }) => {
  await page.locator('.dynamic-button').click();
  // Playwright automatically waits for:
  // 1. Element to be attached to DOM
  // 2. Element to be visible
  // 3. Element to be stable (no animations)
  // 4. Element to receive events
});

3. Wait for element to be removed

// RIGHT — wait for disappearance
test('wait for loading to finish', async ({ page }) => {
  await page.locator('#loading').waitFor({ state: 'hidden' });
  // Or detach:
  await page.locator('#loading').waitFor({ state: 'detached' });
});

4. Wait for attribute changes

// RIGHT — wait for specific condition
test('wait for enabled button', async ({ page }) => {
  const submitBtn = page.locator('button[type="submit"]');

  // Wait for button to be enabled
  await expect(submitBtn).toBeEnabled({ timeout: 10000 });
  await submitBtn.click();
});

Expected output:

✓ wait for visible element (1.5s)
✓ auto-wait for click (0.8s)
✓ wait for loading to finish (2.1s)

Prevention Tips

  • Use locator.waitFor({ state }) for explicit waits
  • Rely on Playwright's auto-waiting for most actions
  • Use expect(locator).toBeVisible() for implicit waits
  • Set appropriate timeouts for async content
  • Prefer locator methods over waitForSelector

Common Mistakes with wait for selector

  1. Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
  2. Non-exhaustive pattern matches that compile with warnings then crash at runtime
  3. Misunderstanding that String is [Char] with poor performance for large text operations

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 states can I wait for with waitFor?

attached — element exists in DOM. detached — element removed from DOM. visible — element is visible. hidden — element is not visible. Default is visible.

Should I use waitForSelector or locator methods?

Use locator methods (.click(), .fill(), etc.) which auto-wait. Use locator.waitFor() when you need to wait for a state before performing an action that doesn't auto-wait.

What's the difference between waitFor and expect(locator).toBeVisible?

waitFor({ state: 'visible' }) waits and returns. expect(locator).toBeVisible() waits and asserts. Use expect(locator).toBeVisible() when you want to verify visibility, waitFor when you just need to wait.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro