Puppeteer log inside page.evaluate

后端 未结 10 1010
野的像风
野的像风 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:49

    I am trying to share my workaround if it helps anybody in future.

    1. Print all the console outputs to stdout including warning, error, log:

      page = await browser.newPage();
      page.on("console", (consoleObj) => console.log(consoleObj.text()));
      
    2. Print everything except warning:

      page.on('console', consoleObj => {
          if (consoleObj.type() !== 'warning') {
              console.log(consoleObj.text());
          }
      })
      
    3. Print only logs (Ex: console.logs).

      page.on('console', consoleObj => {
          if (consoleObj.type() === 'log') {
              console.log(consoleObj.text());
          }
      })
      

    The last one helped me more to debug efficiently.

提交回复
热议问题