How to monitor network requests in Puppeteer?

You can monitor network requests in Puppeteer by using the page.on('request') and page.on('response') events. Here's how you can do it:

  1. First, launch a new browser and create a new page:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); // Your code to monitor network requests goes here await browser.close(); })();
  1. Add event listeners for the request and response events to monitor network requests:
page.on('request', request => { console.log(`Request URL: ${request.url()}`); }); page.on('response', response => { console.log(`Response URL: ${response.url()}`); });
  1. You can also log additional information about the requests and responses by accessing the request and response objects:
page.on('request', request => { console.log(`Request URL: ${request.url()}`); console.log(`Request Method: ${request.method()}`); console.log(`Request Headers: ${JSON.stringify(request.headers())}`); }); page.on('response', response => { console.log(`Response URL: ${response.url()}`); console.log(`Response Status: ${response.status()}`); console.log(`Response Headers: ${JSON.stringify(response.headers())}`); });
  1. Run your Puppeteer script and you should see the network requests being logged to the console.

By using these event listeners, you can monitor network requests in Puppeteer and analyze the requests and responses made by your application.