How to set a timeout for page load in Puppeteer?

You can set a timeout for page load in Puppeteer by using the page.setDefaultTimeout method. This method allows you to specify the maximum amount of time in milliseconds that Puppeteer should wait for a page to load before throwing a timeout error.

Here is an example of how to set a timeout for page load in Puppeteer:

const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Set the timeout for page load to 10 seconds await page.setDefaultTimeout(10000); try { await page.goto('https://example.com'); console.log('Page loaded successfully'); } catch (error) { console.error('Timeout error: Page load took too long'); } await browser.close(); })();

In this example, we set the timeout for page load to 10 seconds using page.setDefaultTimeout(10000). If the page does not load within the specified timeout, a timeout error will be thrown and caught in the catch block.