How to handle timeouts in Puppeteer?

Handling timeouts in Puppeteer can be done in a few ways. Puppeteer is a Node.js library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. It's often used for web scraping, automated testing of web pages, and taking screenshots or generating PDFs of pages. Below are some ways to handle timeouts in Puppeteer:

1. Using page.setDefaultNavigationTimeout(timeout) or page.setDefaultTimeout(timeout):

You can set a default timeout for all Puppeteer operations by using the setDefaultNavigationTimeout or setDefaultTimeout function.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Set the default timeout to 30 seconds
  page.setDefaultNavigationTimeout(30000);

  await page.goto('https://www.example.com');

  await browser.close();
})();

2. Using page.goto(url, {timeout: timeout}):

You can also specify a timeout for a specific operation. For example, you can specify a timeout for the goto function which navigates to a URL.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  // Specify a timeout of 30 seconds for this operation
  await page.goto('https://www.example.com', {timeout: 30000});

  await browser.close();
})();

3. Handling timeouts with a try-catch block:

You can handle timeouts by using a try-catch block. If a timeout error occurs, you can catch it and handle it appropriately.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();

  try {
    await page.goto('https://www.example.com', {timeout: 30000});
  } catch (error) {
    console.error('Timeout error', error);
  }

  await browser.close();
})();

In the above examples, the timeout is specified in milliseconds. Therefore, a timeout of 30000 milliseconds is equal to 30 seconds.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon