Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console.log() from page.evaluate() not working #1944

Closed
Nicolas-vrcc opened this issue Feb 1, 2018 · 16 comments
Closed

Console.log() from page.evaluate() not working #1944

Nicolas-vrcc opened this issue Feb 1, 2018 · 16 comments

Comments

@Nicolas-vrcc
Copy link

Hello,

I'm trying to console.log() in a page.evaluate() function and it's not working...
Any solutions ?

Thank you !

@Everettss
Copy link

It is working but it's not outputting to node environment console. Have a look at page.on('console', callback). Here is an example from docs:

page.on('console', msg => {
  for (let i = 0; i < msg.args.length; ++i)
    console.log(`${i}: ${msg.args[i]}`);
});
page.evaluate(() => console.log('hello', 5, {foo: 'bar'}));

@c094728
Copy link

c094728 commented May 4, 2018

everything that is run inside the page.evaluate function is done in the context of the browser page. The script is running in the browser not in node.js so if you log it will show in the browsers console which if you are running headless you will not see. You also can't set a node breakpoint inside the function.

@ChristosChristofidis
Copy link

ChristosChristofidis commented May 11, 2018

you can capture it with page.on('console' even in headless

@proton1k
Copy link

@Everettss thanks for the example, but It does not seem to work with evaluateHandle though. And neither for evaluate method in my case. Could you please explain the current case how page.on carries .log call ? Or better to read the source code for page.on ?

@hungcao181
Copy link

Page.on('console', (msg) => {}) actually fired, but the example will not work because msg.args is undefined. Inside the callback, just console.log(msg) we will see what is inside.
Actually msg is of type ConsoleMessage, which provide 3 methods args(), text(), type()
We should call these 3 methods inside the callback instead of just access them as properties.
https://pptr.dev/#?product=Puppeteer&version=v1.7.0&show=api-class-consolemessage

@hungcao181
Copy link

Just one more thing, the page.on('console', callback) should be registered before any console.log statement can be called

@GuoZhaoHui628
Copy link

谢谢 @c094728 解答,如果还有问题,戳右边链接。https://guozh.net/puppeteer-page-evaluate-consolelog-not-working/

@ghost
Copy link

ghost commented Nov 13, 2019

For anyone looking at this issue now, in the latest version, you'll need to do this instead:

for (let i = 0; i < msg._args.length; ++i)
    console.log(`${i}: ${msg._args[i]}`);

args has been replaced with _args

@asamountain
Copy link

page.on('console', msg => {
for (let i = 0; i < msg._args.length; ++i)
    console.log(`${i}: ${msg._args[i]}`);
});

as referred, @Ed-Ed @Everettss above code does work!
Thank you guys

@ezy
Copy link

ezy commented Feb 20, 2020

For the full message logs this worked for me:

page.on("console", msg => console.log("PAGE LOG:", msg));

@iniatse
Copy link

iniatse commented May 22, 2020

this does not trigger for console.log() within the site which is loaded. It only triggers when using page.evaluate()

@nickofthyme
Copy link

Slight improvement to @ezy's solution. This will print the message type and the resolved message text from the console args.

page.on('console', (msg) => console[msg._type]('PAGE LOG:', msg._text));

@whoisjuan
Copy link

whoisjuan commented Oct 1, 2020

I don't know why this hasn't been mentioned here but simply setting dumpio: true when setting puppeteer.launch options will pass up all the console logs inside all your page.evaluate.

@Hamisakim
Copy link

@whoisjuan Thanks!!
if anyone runs into this issue, this is the snippet needed

  const browser = await puppeteer.launch({
    dumpio: true
  })

@lgh06
Copy link

lgh06 commented Sep 21, 2023

in 2023 Sep, I have to use page.on('console', (msg) => console[msg.type()]('PAGE LOG:', msg.text()));

@cantonalex
Copy link

I'm also having this problem seems to have stopped working after 21.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests