添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm trying to get urls of navigation/domain redirects using the Chrome Dev tools Network.requestIntercepted event through Puppeteer, but I cant seem to access any of the events data.

The code below doesn't seem to trigger Network.requestIntercepted and I can't work out why.

Any help appreciated.

// console command
// node chrome-commands.js http://yahoo.com test
var url = process.argv[2];
const puppeteer = require('puppeteer');
puppeteer.launch().then(async browser => {
const page = await browser.newPage();
const client = await page.target().createCDPSession();
await client.send('Network.enable');
await client.on('Network.requestIntercepted', (e) => {
  console.log(e);
  console.log("EVENT INFO: ");
  console.log(e.interceptionId);
  console.log(e.resourceType);
  console.log(e.isNavigationRequest);
  await page.goto(url);
  await browser.close();
                I am pretty sure that .on() does not return a promise, therefore you can't await anything.
– Tomalak
                Mar 10, 2018 at 17:00
                I also use await client.on('Network.requestWillBeSent', (e) => { }); the same way and that works fine. github.com/GoogleChrome/puppeteer/blob/v1.1.1/docs/…
– turrican_34
                Mar 10, 2018 at 17:07

You should configure Network.setRequestInterception before Network.requestIntercepted. Here is working example:

const url = 'http://yahoo.com';
const puppeteer = require('puppeteer');
puppeteer.launch({ userDataDir: './data/' }).then(async browser => {
  const page = await browser.newPage();
  const client = await page.target().createCDPSession();
  await client.send('Network.enable');
  // added configuration
  await client.send('Network.setRequestInterception', {
    patterns: [{ urlPattern: '*' }],
  await client.on('Network.requestIntercepted', async e => {
    console.log('EVENT INFO: ');
    console.log(e.interceptionId);
    console.log(e.resourceType);
    console.log(e.isNavigationRequest);
    // pass all network requests (not part of a question)
    await client.send('Network.continueInterceptedRequest', {
      interceptionId: e.interceptionId,
  await page.goto(url);
  await browser.close();
                Thanks, that's got it working. If I add  if (!e.isNavigationRequest) {      return;     } to Network.requestIntercepted it hangs though. Any idea why?
– turrican_34
                Mar 12, 2018 at 10:32
                You should set errorReason in continueInterceptedRequest config object like this: errorReason: e.isNavigationRequest ? undefined : 'Aborted'
– Everettss
                Mar 12, 2018 at 10:57
                @turrican_34 I don't know what you are trying to achieve. But I'm sure that your code must reach Network.continueInterceptedRequest and returning before it can hang whole process.
– Everettss
                Mar 12, 2018 at 11:43
                It wasn't hanging straight away, but after navigation redirects where retrieved when it should have closed. Using errorReason, it's working fine now though.
– turrican_34
                Mar 12, 2018 at 11:54
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.