To capture console logs from a page in Puppeteer, you can use the console
event provided by the Browser object. Here is an example code snippet to capture console logs:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Listen for console logs on the page
page.on('console', msg => {
console.log(`Page Log: ${msg.text()}`);
});
// Navigate to a website
await page.goto('https://example.com');
// Close the browser
await browser.close();
})();
In the code above, we are using the page.on('console', ...)
method to listen for console logs on the page. When a console log event is triggered, we are printing the log message to the console using msg.text()
. You can modify the code to store the logs in a file or perform any other desired action with the console logs.