User Agents

Web browsers and everyday web tech, explained

How to Set Timeouts for Headless Chrome in Puppeteer Playwright and Selenium

· 5 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Setting timeouts for headless Chrome in Puppeteer, Playwright, and Selenium prevents scripts from hanging indefinitely when pages load slowly, network requests stall, or elements don't appear. Each tool has its own timeout methods, but the core idea is the same: define a maximum wait time for actions, navigation, and script execution. This guide covers the exact code and configuration for all three libraries.

What You'll Need

  • Node.js installed (for Puppeteer and Playwright) and Python (for Selenium, though Selenium also supports Java, C#, etc.)
  • Basic familiarity with your chosen automation library (installed via npm or pip)
  • A headless Chrome browser (Puppeteer and Playwright download Chromium automatically; Selenium needs ChromeDriver matching your Chrome version)

Step-by-Step

Puppeteer

  1. Set a global default timeout for all actions. In your Puppeteer script, after launching the browser, use page.setDefaultTimeout(milliseconds). This applies to most operations like waitForSelector, click, and type.
  2. Set a navigation timeout separately. Use page.setDefaultNavigationTimeout(milliseconds) to control how long page.goto() and page.reload() wait. This is often set to a higher value than the action timeout because page loads can take longer.
  3. Override timeouts per action when needed. For a single call, pass a timeout option in milliseconds. For example: await page.click('button', { timeout: 5000 }) or await page.goto(url, { timeout: 10000 }).
  4. Set a script-wide timeout for the entire Node.js process. Use setTimeout(() => process.exit(1), 60000) as a safety net, but be careful—this will kill the process even if the script is working fine.

Playwright

  1. Set default timeouts in the browser context. Playwright uses browserContext.setDefaultTimeout(milliseconds) for all actions and browserContext.setDefaultNavigationTimeout(milliseconds) for navigation. If you create a new page, it inherits these.
  2. Use per-action timeouts. Every Playwright method accepts a timeout option. For example: await page.click('button', { timeout: 5000 }) or await page.goto(url, { timeout: 10000 }).
  3. Set global timeouts in the test runner. If you use Playwright Test, configure timeout in the playwright.config.js file under use or at the test level. This controls how long an entire test can run.
  4. Handle timeouts with try/catch. Playwright throws a TimeoutError when a timeout is exceeded. Catch it to log a meaningful message or take a screenshot.

Selenium (Python example)

  1. Set implicit wait. After creating the WebDriver instance, use driver.implicitly_wait(10) (in seconds). This tells the driver to wait up to 10 seconds for an element to appear before throwing an exception. It applies to all element-finding calls.
  2. Set page load timeout. Use driver.set_page_load_timeout(30) to limit how long driver.get(url) waits for the page to load.
  3. Set script timeout. Use driver.set_script_timeout(30) to limit how long driver.execute_async_script() can run.
  4. Use explicit waits for specific conditions. Import WebDriverWait and expected_conditions. For example: WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'myId'))). This is more precise than implicit wait and is recommended for most cases.
  5. Set timeout in Selenium WebDriver options (Java/C#). If you're not using Python, the same concepts apply: driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS) in Java, or driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10) in C#.

Common Mistakes

  • Setting implicit wait too high. A high implicit wait (e.g., 30 seconds) makes every element lookup slow, even when the element is present immediately. Use a low implicit wait (5 seconds) and rely on explicit waits for critical elements.
  • Confusing implicit and explicit waits. Implicit wait applies globally; explicit wait applies to a single condition. Using both together can cause unexpected delays because the total wait time is the sum of both.
  • Setting navigation timeout to zero. In Puppeteer, setDefaultNavigationTimeout(0) disables the timeout, which can cause the script to hang forever. Similarly, in Selenium, set_page_load_timeout(0) means no timeout.
  • Not catching timeout exceptions. If you don't catch the timeout error, the script crashes with a stack trace, which is hard to debug. Always wrap time-sensitive operations in try/catch.
  • Using the same timeout for all actions. Navigation usually takes longer than clicking a button. Set a longer navigation timeout and a shorter action timeout.

Tips

  • Start with a reasonable default. For most sites, 30 seconds for navigation and 10 seconds for actions is a good starting point. Adjust based on your site's speed.
  • Use explicit waits in Selenium. They are more reliable than implicit waits because they wait for a specific condition (e.g., element visible, clickable) rather than just presence.
  • In Puppeteer and Playwright, prefer waitForSelector with a visible state. This waits for the element to be in the DOM and visible, which is often what you need before interacting.
  • Set a global timeout in your test runner. For Playwright Test, set timeout: 60000 in the config to prevent a single test from running forever.
  • Log timeout errors with context. Include the URL and the action that timed out in your error message to make debugging easier.

Troubleshooting

  • Timeout still occurs even after increasing the value. This may be due to a slow network or a page that never finishes loading (e.g., infinite scrolling). Check if the page has a resource that never loads, and consider using waitUntil: 'domcontentloaded' instead of 'load' in Puppeteer/Playwright.
  • Element not found despite timeout. The selector might be wrong, or the element is inside an iframe. Use page.frames() in Puppeteer/Playwright to switch to the correct frame, or in Selenium use driver.switch_to.frame().
  • Script hangs indefinitely. If you set a timeout to 0 (disabling it), the script may never exit. Always set a non-zero timeout, and add a process-level timeout as a fallback.
  • Playwright Test times out before your action timeout. The test runner's timeout is separate from action timeouts. Increase the test timeout in the config file.
  • Selenium implicit wait not working as expected. Implicit wait only applies to element finding, not to page load. If the page load is slow, set set_page_load_timeout separately.

Frequently Asked Questions

What is the difference between implicit and explicit wait in Selenium?

Implicit wait applies a global timeout to every element lookup. Explicit wait allows you to wait for a specific condition (like visibility) on a particular element. Use explicit wait for better control.

Can I set a timeout for the entire script in Puppeteer?

Yes, you can use page.setDefaultTimeout() for actions, but for the whole script, you need to add your own timer, like setTimeout(() => process.exit(1), 60000).

What happens when a timeout is exceeded in Playwright?

Playwright throws a TimeoutError. You can catch it and handle it gracefully, such as taking a screenshot or retrying the action.

Should I set timeouts to zero to disable them?

No, setting timeouts to zero disables them, which can cause your script to hang indefinitely. Always use a positive number.

Why does my navigation timeout not work in Selenium?

Make sure you call driver.set_page_load_timeout() before calling driver.get(). Also, some browsers may ignore this if the page has a slow-loading resource.

Can I use the same timeout for all actions?

It's not recommended. Navigation and element interactions have different performance characteristics. Set separate, appropriate values.