Playwright Navigation Wait Not Working Fix
In this tutorial, you'll learn about Playwright Navigation Wait Not Working Fix. We cover key concepts, practical examples, and best practices.
Your test proceeds before the page finishes navigating — elements are missing because the new page hasn't loaded yet.
The Problem
// WRONG — clicking triggers navigation but test doesn't wait
test('navigate to profile', async ({ page }) => {
await page.getByText('Profile').click();
// Navigation starts but doesn't complete before next line
await expect(page.locator('h1')).toHaveText('Profile');
// May fail — old page's h1 or no h1 at all
});
The click triggers navigation, but the test immediately tries to find h1 on the current page. If the navigation hasn't completed, the assertion fails.
Step-by-Step Fix
1. Use waitForURL after navigation
// RIGHT — wait for URL change
import { test, expect } from '@playwright/test';
test('navigate to profile', async ({ page }) => {
await page.goto('https://example.com');
await page.getByText('Profile').click();
// Wait for URL to contain /profile
await page.waitForURL('**/profile');
await expect(page.locator('h1')).toHaveText('User Profile');
});
2. Use waitForLoadState
// RIGHT — wait for page load state
test('navigate and wait for load', async ({ page }) => {
await page.getByText('Profile').click();
await page.waitForLoadState('networkidle');
await expect(page.locator('h1')).toBeVisible();
});
3. Use promise-based navigation
// RIGHT — wait for navigation to complete
test('navigation via promise', async ({ page }) => {
await page.goto('https://example.com');
// Start waiting for navigation before triggering it
const navigationPromise = page.waitForURL('**/profile');
await page.getByText('Profile').click();
await navigationPromise;
});
4. Handle SPA navigations
// RIGHT — SPA navigation handling
test('SPA navigation', async ({ page }) => {
await page.goto('https://spa.example.com');
// For SPAs that don't trigger full page loads
await page.getByText('Dashboard').click();
await page.waitForURL('**/dashboard');
// Or wait for a specific element to appear
await expect(page.locator('.dashboard-content')).toBeVisible();
});
Expected output:
✓ navigate to profile (1.2s)
✓ navigate and wait for load (1.0s)
✓ navigation via promise (1.1s)
Prevention Tips
- Use
page.waitForURL()after navigation-triggering actions - Use
page.waitForLoadState('networkidle')for heavy pages - Start waiting before the action for race condition safety
- Use locator-based waits when URL doesn't change (SPA)
- Set appropriate timeouts for slow navigations
Common Mistakes with wait for nav
- 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