Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop puppeteer follow redirects

Currently it seems the default behaviour of puppeteer is to follow redirects and return the DOM at the end of the chain.

How can I make the .goto() method to stop after the first redirect happened and simply return the html from that first 3xx page when i call page.content() method?

like image 611
Ali Habibzadeh Avatar asked Oct 23 '17 10:10

Ali Habibzadeh


2 Answers

You can enable a request interception and abort additional requests if a chain of requests is identified:

await page.setRequestInterception(true);

page.on('request', request => {
  if (request.isNavigationRequest() && request.redirectChain().length !== 0) {
    request.abort();
  } else {
    request.continue();
  }
});

await page.goto('https://www.example.com/');
like image 86
Grant Miller Avatar answered Nov 19 '22 12:11

Grant Miller


It seems that at the moment of writing, this is not possible (at least not in the high-level API that Puppeteer provides). Check out the docs for goto here.

like image 1
tomahaug Avatar answered Nov 19 '22 11:11

tomahaug