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:
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();
})();
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()}`);
});
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())}`);
});
By using these event listeners, you can monitor network requests in Puppeteer and analyze the requests and responses made by your application.