0

Puppeteer timeout

Timeout sets the maximum timeout for a particular method or methods after which an error or exception will be thrown.

  1. Puppeteer timeout default
  2. Method setDefaultNavigationTimeout
  3. Method setDefaultTimeout
  4. Timeout property
  5. WaitUntil property
  6. Puppeteer timeout promise

Puppeteer timeout default

The default in puppeteer timeout is 30 seconds. To use custom timeouts, you can use the page.setDefaultNavigationTimeout () and page.setDefaultTimeout () methods or the timeout property in the options parameter. The wait time in all cases is specified in milliseconds.

In all of the following methods in puppeteer, the timeout can be set to 0, i.e. disable timeout.

Method setDefaultNavigationTimeout

Changes the maximum timeout for the following methods:

page.goBack()
page.goForward()
page.goto()
page.reload()
page.setContent()
page.waitForNavigation()

To set the timeout to 60 seconds, you must do:

await page.setDefaultNavigationTimeout(60000);

You can use the method after the page is declared:

const puppeteer = require('puppeteer');          // puppeteer

(async () => {
const browser = await puppeteer.launch();        // run browser
const page = await browser.newPage();            // open new tab
await page.setDefaultNavigationTimeout(60000);   // change timeout
...                                              // your code
await browser.close();                           // close browser
})();

Method setDefaultTimeout

Changes the maximum timeout for the following methods:

page.goBack()
page.goForward()
page.goto()
page.reload()
page.setContent()
page.waitFor()
page.waitForFileChooser()
page.waitForFunction()
page.waitForNavigation()
page.waitForRequest()
page.waitForResponse()
page.waitForSelector()
page.waitForXPath()

To set timeout to 45 seconds, you must do:

await page.setDefaultTimeout(45000);

You can also use the method only after the page is declared:

const puppeteer = require('puppeteer');          // puppeteer

(async () => {
const browser = await puppeteer.launch();        // run browser
const page = await browser.newPage();            // open new tab
await page.setDefaultTimeout(45000);             // change timeout
...                                              // you code
await browser.close();                           // close browser
})();

Note that the setDefaultNavigationTimeout method has higher priority than setDefaultTimeout!

Timeout property

If you want to set the puppeteer timeout for only one of the page methods (page.goto(), page.waitFor(), page.waitForSelector(), etc.), then this can be done via the timeout property of the options parameter (with full a list of methods in which this property is available can be found in the documentation).

For example, to change the maximum timeout when loading a page, you must specify the timeout property when using this method:

await page.goto('<site url>', {timeout: ms});

For example, loading a google page with a maximum latency of 45 seconds:

const puppeteer = require('puppeteer');                  // puppeteer

(async () => {
const browser = await puppeteer.launch();                // run browser
const page = await browser.newPage();                    // open new tab
await page.goto('https://google.com', {timeout: 45000}); // change timeout
...                                                      // you code
await browser.close();                                   // close browser
})();

WaitUntil property

Using this property, you can expect a full page / document load or lack of network activity. The following values ​​are possible (default is load):

load – consider navigation to be finished when the load event is fired.
domcontentloaded – consider navigation to be finished when the DOMContentLoaded event is fired.
networkidle0 – consider navigation to be finished when there are no more than 0 network connections for at least 500 ms.
networkidle2 – consider navigation to be finished when there are no more than 2 network connections for at least 500 ms.

This property is present in the parameters of the following methods:

page.goBack()
page.goForward()
page.goto()
page.reload()
page.setContent()
page.waitForNavigation()

For example, to make sure that the page has loaded, all scripts have finished running, etc. you can use the networkidle0 property:

const puppeteer = require('puppeteer');                  // puppeteer

(async () => {
const browser = await puppeteer.launch();                // run browser
const page = await browser.newPage();                    // open new tab
await page.goto('https://google.com', {waitUntil: networkidle0}); // wait lack of network activity
...                                                      // you code
await browser.close();                                   // close browser
})();

Puppeteer timeout promise

In this article, we expected a popup to appear. But if it does not open, then the script will not continue its execution. To avoid this, you need to set a time limit on the promise. To do this, I wrote the following function, in which promise and the maximum timeout are passed. If an event has occurred, then the function returns the result, otherwise the value is ‘1’:

async function timeoutPromise(promise, ms){
  timeout = new Promise(function(resolve, reject){
        setTimeout(resolve, ms, 1);
  });
  result = Promise.race([promise, timeout]).then(function(value){
        return value;
  });
  return result;
}

Let’s add our code:

const puppeteer = require('puppeteer');         // puppeteer

// function timeout promise
async function timeoutPromise(promise, ms){
  timeout = new Promise(function(resolve, reject){
        setTimeout(resolve, ms, 1);
  });
  result = Promise.race([promise, timeout]).then(function(value){
        return value;
  });
  return result;
}

(async () => {
const browser = await puppeteer.launch();       // run browser
const page = await browser.newPage();           // open new tab
await page.goto('https://site.com');            // go to site.com       

await page.waitForSelector('#goto');            // wait object load
const link = await page.$('#goto');             // declare object

const newPagePromise = new Promise(x => page.once('popup', x));

await link.click();                             // click, a new tab must opens
newPageResult = await timeoutPromise(newPagePromise, 5000); // set timeout to promise 5 sec
const newPage = await newPagePromise;           // declare new tab /window, 
now you can work with it
await newPage.close();                          // close it, for example
...
await browser.close();                          // close browser
})();