javascriptnode.jspuppeteerconsole.logheadless-browser

Need to be able to print data, created in node.js headless browser


How to do workaround to be able to print out any data, eg. instant string, variables, etc, created inside node.js running a headless browser as console.log('Text') won't work.
such e.g. is

 const pup = require('puppeteer');
 const chrom = await pup.launch({headless:'new'});
 const page = await chrom.newPage();
 await page.goto(aURI);

 await page.evaluate(() => {

              // <= How the code line to achieve this need

});

Solution

  • From the debugging related page of official puppeteer documentation.

    Since client code runs in the browser, doing console.* in client code will not directly log to Node.js. However, you can listen for the console event which returns a payload with the logged text.

    page.on('console', msg => console.log('PAGE LOG:', msg.text()));
    
    await page.evaluate(() => console.log(`url is ${location.href}`));