1

How can I output console logs from page.evaluate to local terminal?

await page.evaluate(() => {
    console.log("test"); // <-- I want this message to show in my local terminal

I am running my code locally on a MacOS computer.

The following solutions I have found in github issues have not worked:

solution 1

const browser = await puppeteer.launch({
    'args': ['--disable-dev-shm-usage', '--disable-software-rasterizer'],
    dumpio: true
});

// output:
// ERROR:gpu_process_transport_factory.cc(967)] Lost UI shared context.

solution 2

const browser = await puppeteer.launch({
    'args': ['--disable-dev-shm-usage', '--disable-software-rasterizer']
});

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

// output:
// nothing
user1283776
  • 19,640
  • 49
  • 136
  • 276

1 Answers1

0

Just started with Puppeteer myself. Couldn't figure it out right away, but after trying a few things, this worked for me.

const names = await page.evaluate(() => {
        let names = document.querySelectorAll("#lnkBEName");
        console.log(names);
        return names;
    })
    console.log(names);

Then console.log inside the function block doesn't work. However, the last line, outside the block works. Hope this helps someone.