Playwright Element Wait Not Working Fix
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
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[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
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro