Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
419 views
in Technique[技术] by (71.8m points)

javascript - How do print the console output of the page in puppeter as it would appear in the browser?

I keep seeing this WRONG CODE

page.on('console', msg => console.log(msg.text()));

That FAILS

console.log('Hello %s', 'World');

produces

Hello World     // browser
Hello %s World  // puppeteer

Ok, So I thought maybe I could do this

page.on('console', msg => console.log(...msg.args()));

NOPE: That dumps out some giant JSHandle thing.

Ok, So maybe

page.on('console', msg => console.log(...msg.args().map(a => a.toString());

NOPE: That prints

JSHandle: Hello %s JSHandle: World

I suppose I can hack it by removing the first 9 characters.

I also tried

page.on('console', msg => console.log(...msg.args().map(a => a.jsonValue())));

NOPE: That prints

Promise { <pending> } Promise { <pending> }

Okay how about

page.on('console', async(msg) => {
  const args = Promise.all(msg.args().map(a => a.jsonValue()));
  console.log(...args);
});

Nope, That prints

UnhandledPromiseRejectionWarning: TypeError: Found non-callable @@iterator
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 95)

Another go

page.on('console', async(msg) => {
  const args = Promise.all(msg.args().map(async(a) => {
    return await a.jsonValue();
  }));
  console.log(...args);
});

same as before

UnhandledPromiseRejectionWarning: TypeError: Found non-callable @@iterator
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a 

I even traced through msg.text() but all it does is return a premade string so it's too late to use that.

How do I get the same output as the browser's console.log in puppeteer?


PS: as mentioned above, this hack works in puppeteer 1.20.0

  page.on('console', (msg) => {
    console.log(...msg.args().map(v => v.toString().substr(9)));
  });

but it's clearly a hack and I expect it will break at some point so looking for the correct solution.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  page.on('console', async e => {
    const args = await Promise.all(e.args().map(a => a.jsonValue()));
    console.log(...args);
  });

or

  page.on('console', async e => {
    const args = await Promise.all(e.args().map(a => a.jsonValue()));
    console[e.type() === 'warning' ? 'warn' : e.type()](...args);
  });

works


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...