ScreenshotNeo

BlogHow-to

How to Get the Text of a Puppeteer Console Message

Read a Puppeteer console message with `msg.text()`. Learn how to capture it, inspect its arguments and type, and troubleshoot common logging issues.

By the ScreenshotNeo team4 October 20264 min read

Listen for the Puppeteer page’s console event and call text() on the ConsoleMessage passed to the callback:

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

msg.text() returns a string containing the console message’s text. The callback value is a Puppeteer ConsoleMessage object, not a string. See the official ConsoleMessage.text() reference and ConsoleMessage class reference.

Runnable example

This complete Node.js example opens a page, subscribes before navigation, and prints each console message’s type and text:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch({ headless: true });
  try {
    const page = await browser.newPage();

    page.on('console', msg => {
      console.log(`[${msg.type()}] ${msg.text()}`);
    });

    await page.goto('https://example.com', { waitUntil: 'domcontentloaded' });
  } finally {
    await browser.close();
  }
})();

Install Puppeteer in the project with npm install puppeteer, save the example as console.js, then run node console.js. Register the listener before the action that may produce console output, such as navigation or clicking a button.

Choose the right message accessor

What you need Use What it gives you
Readable message text msg.text() A string representation of the console message.
Original logged values msg.args() The console arguments as handles, useful when a page logs multiple values or objects.
Message category msg.type() The type, such as a log level or warning/error category.
Source location msg.location() Available location metadata for the message.
Stack information msg.stackTrace() Stack trace information when available.

Use text() when a plain string is enough. It is not a structured array of the original arguments. For structured inspection, use args() and handle the returned JS handles appropriately. The class is created and emitted by Puppeteer; its constructor is internal, so application code should not instantiate ConsoleMessage directly.

Filter or collect messages

To print only errors, inspect the type in the same event callback:

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

To collect text for later assertions or reporting:

const messages = [];
page.on('console', msg => messages.push(msg.text()));

await page.goto('https://example.com');
console.log(messages);

Attach the listener before navigation or before triggering page code. If you only need to capture a specific action, attach it immediately before that action and remove it afterward with page.off('console', handler) when the handler is no longer needed.

What the event includes

Puppeteer emits the page’s console event with a ConsoleMessage for page console activity, including messages such as warnings and errors. The event payload is the place to obtain message text and related metadata. Consult the official PageEvent documentation for the event reference.

Or skip the browser setup

If your goal is a screenshot rather than inspecting browser logs, ScreenshotNeo is a website screenshot API and MCP server. One GET request captures a URL as an image or PDF; see the API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp

ScreenshotNeo accepts cookie and consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before the capture; each step can be turned off. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and response headers report the page verdict and billing status. Its MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 screenshots.

Create a free ScreenshotNeo account and get 1,000 screenshots a month with no card.

Troubleshooting

Symptom Likely cause Fix
msg.text is not a function The event value was treated as a string or is not the Puppeteer event payload. Attach to page.on('console', msg => ...) and call msg.text() on that payload.
No messages appear The listener was attached after the page already logged, or the action that produces output has not run. Register the listener before navigation or the interaction, then trigger the page code.
Objects appear as text that is hard to inspect text() provides a text representation, not the original structured values. Use msg.args() when you need to inspect the individual logged arguments.
Too much output The handler records all console message types. Check msg.type() and filter to the categories relevant to your task.

Performance and reliability notes

  • Keep the event handler small; expensive synchronous work in it can slow your Node.js process while the page emits messages.
  • Register before navigation when startup messages matter. A listener cannot recover events emitted before it was attached.
  • Remove temporary listeners after the relevant operation to avoid accumulating duplicate handlers during repeated captures.
  • Console output depends on what the page executes. A page that produces no console calls will not produce corresponding messages.

FAQ

What does ConsoleMessage.text() return?

A string containing the console message’s text.

Can I get the logged values instead of their text?

Yes. Use msg.args() to access the original console arguments and inspect them as needed.

Should I create a ConsoleMessage instance myself?

No. Puppeteer constructs and emits these objects through the page event; the documented constructor is internal.

Does the console event include warnings and errors?

Yes. The event reference describes console activity that includes page errors and warnings; use msg.type() to distinguish message types.