How can I console.log something inside the page.evaluate, passing it to node and using it during the evaluation of the page?
I actually want to log
**Updated to work with puppeteer v1.4.x
If all you want is to "log the progress of the page.evaluate to the console", then just
const page = await browser.newPage();
page.on('console', consoleObj => console.log(consoleObj.text()));
And use console.log
in page.evaluate
as usual, no more dependencies are required.
Also see this nice tweak to remove multiple annoying warnings from log.
const page = await browser.newPage();
page.on('console', ConsoleMessage => console.log(ConsoleMessage.text));
I like @Vaviloff's answer, but you will log the whole ConsoleMessage object when you may just want the text. Thus, I personally use the below:
const EOL = require('os').EOL;
const _page = await browser.newPage();
_page.on('console', _fCleanLog);
function _fCleanLog(ConsoleMessage) {
console.log(ConsoleMessage.text + EOL);
}
Implement the notifyUi
function in this code sample:
const page = await browser.newPage();
page.on('console', (...args) => {
this.notifyUi('[chrome] ' + args[0]);
});
await page.goto(url);
const result = await page.evaluate(() => {
console.log('I am alive');
return Promise.resolve(true);
});
this.notifyUi('Evaluation returned with ' + result);