Puppeteer log inside page.evaluate

后端 未结 10 1049
野的像风
野的像风 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 04:50

    A lot of the answers provided previously no longer work today. Also one thing that can be very annoying on some pages, is the "warning" messages which pollutes the output. One way to fix that is to filter for the type of the message. The following code helps reduce the noise and works with current versions of Puppeteer:

    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    page.on('console', consoleMessageObject => function (consoleMessageObject) {
        if (consoleMessageObject._type !== 'warning') {
            console.debug(consoleMessageObject._text)
        }
    });
    
    await page.goto('https://google.com');
    const result = await page.evaluate(() => {
        console.log('Browser scope.');
        return 'Normal scope.';
    });
    console.log(result)
    

提交回复
热议问题