Last updated 2023-08-09 18:19:05

Puppeteer: Fix Timeout Issue

By default Puppeteer [↗] will timeout and stop loading a page if it takes too long.

To fix this you will have to specify the maximum time it needs to wait for the page to load before timing out. The tricky bit is that occasionally some pages take a while to load, which means regardless of the duration you set, from time to time your page might time out.

You can setup your Puppeteer script to never timeout :

01: const puppeteer = require("puppeteer"); 02: puppeteer 03: .launch({ 04: defaultViewport: { 05: width: 1280, 06: height: 2000, 07: }, 08: }) 09: .then(async (browser) => { 10: const page = await browser.newPage(); 11: await page.goto("websiteurl.com"); 12: await page.screenshot({ path: "image1.png" }); 13: await page.setDefaultNavigationTimeout(0); 14: await browser.close(); 15: });

On line 13 we provide the setDefaultNavigationTimeout(0); method with 0 this will stop it from ever timing out.

Depending on what you use the script for, you might want to at least set a timeout so your script does not crush your server.

Here is another article you might like 😊 "Regex For Validating Emails|explained"