TimeoutError
asyncio.exceptions.TimeoutError
Stack trace
asyncio.exceptions.TimeoutError: Timeout 30000ms exceeded.
File "/usr/local/lib/python3.9/site-packages/playwright/_impl/_page.py", line 1234, in goto
await self._main_frame.goto(url, timeout=timeout)
File "/usr/local/lib/python3.9/site-packages/playwright/_impl/_frame.py", line 567, in goto
raise TimeoutError(f"Timeout {timeout}ms exceeded.") Why it happens
This error occurs when the browser automation script waits for a page to load or navigate but the page does not finish loading within the specified timeout duration. It can happen due to slow network, heavy page resources, or incorrect URL causing the navigation to hang.
Detection
Monitor navigation calls with try/except blocks catching TimeoutError and log the URL and elapsed time to detect slow or stuck page loads before the script crashes.
Causes & fixes
The page takes longer than the default 30 seconds to load due to slow network or heavy resources.
Increase the timeout parameter in the navigation call, e.g., page.goto(url, timeout=60000), to allow more time for the page to load.
The URL is incorrect or leads to a page that never finishes loading (e.g., infinite redirect or server hang).
Verify the URL is correct and accessible. Use page.wait_for_load_state('networkidle') after navigation to confirm page load completion.
The page triggers dialogs, alerts, or requires user interaction blocking navigation completion.
Handle dialogs and popups explicitly in the script using page.on('dialog', handler) to prevent navigation from stalling.
Code: broken vs fixed
from playwright.async_api import async_playwright
import asyncio
async def run():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
# This line causes TimeoutError if page load is slow
await page.goto('https://example.com')
await browser.close()
asyncio.run(run()) import os
from playwright.async_api import async_playwright
import asyncio
async def run():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
# Increased timeout to 60 seconds to prevent navigation timeout
await page.goto('https://example.com', timeout=60000)
await browser.close()
asyncio.run(run()) Workaround
Wrap the navigation call in try/except TimeoutError, then retry navigation with a longer timeout or fallback to a simpler page URL.
Prevention
Use explicit waits for network idle or specific page elements after navigation and increase timeouts based on expected page complexity to avoid premature timeouts.