How to inspect network traffic and get the URL of resource requests?

元气小坏坏 提交于 2019-12-06 01:51:19

Check out the sample that intercepts image requests. Easy to modify that to look at other types of resource requests:

await page.setRequestInterceptionEnabled(true);
page.on('request', request => {
  if (/\.js$/i.test(request.url)) {
    // request for js resource
  }
  request.continue();
});
await page.goto('https://example.com');

Came across this post and SetRequestInterceptionEnabled has been renamed to

page.setRequestInterception(value)

Here is a piece of code i found on the Documentation:

const puppeteer = require('puppeteer');

puppeteer.launch().then(async browser => {
  const page = await browser.newPage();
  await page.setRequestInterception(true);
  page.on('request', interceptedRequest => {
    if (interceptedRequest.url.endsWith('.png') || interceptedRequest.url.endsWith('.jpg'))
      interceptedRequest.abort();
    else
      interceptedRequest.continue();
  });
  await page.goto('https://example.com');
  await browser.close();
});

NOTE Enabling request interception disables page caching.

Here is the URL for the puppeteer Documentation: Puppeteer Documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!