0

I'm having a problem getting the data from a site already exist on the console log on the devoloper tool. I'm new to Puppeteer so I'm trying to get all these console logs results but all of them have a string type. can you guys help me to find a way to get the real object or a way to parse it and use it, thanks

Salah Ben Bouzid
  • 381
  • 4
  • 10
  • Please add more information about what you need to get, with examples, please. It's not clear currently what you mean. Do you want to get data that sites send to `console.log`? Do you want to get some global window objects that exist on the page and you can `console.log` and see them? – Vaviloff Oct 26 '20 at 07:38
  • thanks, yes i when want to get data that sites send to console.log with JSON format so I can use it – Salah Ben Bouzid Oct 26 '20 at 08:30
  • To catch all console.log statements from remote sites try this solution: https://stackoverflow.com/a/46245945/2715393 – Vaviloff Oct 26 '20 at 16:01

1 Answers1

5

We use this to capture console events and log in node:

    page.on('console', async msg => {
      const args = msg.args();
      const vals = [];
      for (let i = 0; i < args.length; i++) {
        vals.push(await args[i].jsonValue());
      }
      console.log(vals.map(v => typeof v === 'object' ? JSON.stringify(v, null, 2) : v).join('\t'));
    });

It checks the type of message being logged and runs JSON.stringify if it is an object.

Todd Price
  • 2,650
  • 1
  • 18
  • 26