Tobiasz Łoś
Tobiasz Łoś

Reputation: 25

Nodejs/puppeteer - Navigation timeout error

Can I resume connection after getting the error like that

UnhandledPromiseRejectionWarning:    
TimeoutError: Navigation Timeout    
Exceeded: 1000ms exceeded

example:

 let arg = [] //array with urls
 await page.goto(...args, {waitUntil: 'load', timeout: 1000 }); 

or the only way out is setup timeout?

Upvotes: 1

Views: 4338

Answers (3)

num8er
num8er

Reputation: 19372

If You want to do request to all urls in args array without stopping loop if one will fail.

So here is solution:

const async = require('async'); // npm i --save async

const urls = [... array of urls ...]; 
const execution = {
  total: urls.length,
  success: 0,
  failed: 0,
  results: []
};

async.eachLimit(
  urls, 
  10, 
  async (url, done) => {
    try {
      const data = await page.goto(url, {waitUntil: 'load', timeout: 1000});
      execution.success++;
      execution.results.push({url, data});
    }
    catch (error) {
      execution.failed++;
      execution.results.push({url, data: null, error: error.message});
    }
    finally {
      done();
    }
  },
  (errors) => {
    console.log('Finished:', execution);
  });

Upvotes: 1

Ondra Urban
Ondra Urban

Reputation: 677

To answer the original question:

No, you cannot resume connection after the page.goto() function times out. You can only handle the exception and perhaps retry.

On the other hand, if what you are trying to accomplish is to load a page,

I suggest two changes to your code:

First:

page.goto() does not accept an Array or an Object as the first argument, it needs to be a singular string, such as:

page.goto('https://www.google.com').

See docs.

Second:

Unless the page you're loading is extremely simple, a timeout of 1000 ms is just too low. Puppeteer's default is 30000 ms, so I suggest either using that or setting a timeout of at least 5000 ms:

page.goto('https://www.google.com', { timeout: 5000 })

There's also no need to use { waitUntil: 'load' } since that is the default value.

Hope this helps.

Upvotes: 1

AJC24
AJC24

Reputation: 3336

I believe your issue comes from the argument you've supplied to the goto method of puppeteer:

https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagegotourl-options

When you invoke goto it expects a string url and not Array<string>.

Upvotes: 0

Related Questions