Playwright Page Navigation Not Working Fix
In this tutorial, you'll learn about Playwright Page Navigation Not Working Fix. We cover key concepts, practical examples, and best practices.
Your Playwright test navigates but the page doesn't load — or the test proceeds before the navigation completes, causing element selection to fail.
The Problem
// WRONG — not awaiting navigation
test('navigate to profile', async ({ page }) => {
page.goto('/profile'); // Missing await
await page.locator('h1').waitFor(); // Fails — navigation hasn't started
});
TimeoutError: page.waitFor: Timeout 5000ms exceeded.
waiting for locator('h1') to be visible
The goto returns a promise that resolves when navigation completes. Without await, the test races ahead.
Step-by-Step Fix
1. Always await navigation
// RIGHT — await page.goto
import { test } from '@playwright/test';
test('navigate to profile', async ({ page }) => {
await page.goto('https://example.com/profile');
await expect(page.locator('h1')).toHaveText('User Profile');
});
2. Use navigation options
// RIGHT — navigation with options
test('navigate with timeout', async ({ page }) => {
await page.goto('https://example.com/profile', {
timeout: 30000,
waitUntil: 'networkidle', // Wait for network to be idle
});
await expect(page).toHaveURL(/.*\/profile/);
});
3. Handle redirects
// RIGHT — follow redirects automatically
test('navigates through redirect', async ({ page }) => {
await page.goto('https://example.com/old-path');
// Playwright follows redirects by default
await expect(page).toHaveURL(/.*\/new-path/);
});
4. Use page.waitForURL for navigation verification
// RIGHT — wait for specific URL
test('click navigates to new page', async ({ page }) => {
await page.goto('https://example.com');
await page.getByText('Go to Profile').click();
await page.waitForURL('**/profile');
await expect(page.locator('h1')).toHaveText('Profile');
});
Expected output:
✓ navigate to profile (1.2s)
✓ navigate with timeout (0.8s)
✓ click navigates to new page (1.5s)
Prevention Tips
- Always
awaitpage.goto() and page.click() that trigger navigation - Use
waitUntiloptions for different loading requirements - Use
page.waitForURL()after navigation-triggering actions - Set appropriate timeouts for slow pages
- Check URL assertions to confirm navigation completed
Common Mistakes with page navigate
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists - 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
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