Puppeteer log inside page.evaluate

后端 未结 10 1008
野的像风
野的像风 2020-12-01 04:34

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

相关标签:
10条回答
  • 2020-12-01 05:07

    **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.

    0 讨论(0)
  • 2020-12-01 05:08
    const page = await browser.newPage();
    page.on('console', ConsoleMessage => console.log(ConsoleMessage.text));
    
    0 讨论(0)
  • 2020-12-01 05:11

    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);
    }
    
    0 讨论(0)
  • 2020-12-01 05:16

    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);
    
    0 讨论(0)
提交回复
热议问题