Puppeteer log inside page.evaluate

Update for puppeteer 12, adapted from the current documentation:

page.on('console', async (msg) => {
  const msgArgs = msg.args();
  for (let i = 0; i < msgArgs.length; ++i) {
    console.log(await msgArgs[i].jsonValue());
  }
});

await page.evaluate(() => console.log('hello', 5));
await page.evaluate(() => console.log({ foo: 'bar' }));
await page.evaluate(() => console.log([1, 2, 3, 4, 5]));

Shows the following results:

hello  
5  
{ foo: 'bar' }  
[ 1, 2, 3, 4, 5 ]  

Leave a Comment