Implementation:Puppeteer Puppeteer ConsoleMessage
| Property | Value |
|---|---|
| sources | packages/puppeteer-core/src/common/ConsoleMessage.ts |
| domains | Console, Debugging, Page Events |
| last_updated | 2026-02-12 00:00 GMT |
Overview
Description
ConsoleMessage is a public class that represents a console message dispatched by a page via the 'console' event. Each instance encapsulates the message type, text content, associated JavaScript handle arguments, stack trace locations, the originating frame, the raw protocol stack trace, and the target ID.
The module also exports the ConsoleMessageLocation interface that describes where in source code a console message originated (URL, line number, and column number), and the ConsoleMessageType type alias which enumerates all 18 supported console message types including 'log', 'error', 'warn', 'debug', 'info', 'table', 'trace', and others.
Usage
ConsoleMessage objects are received by listening to the 'console' event on a Puppeteer Page instance. Users can inspect the message type, text, arguments, and source location to implement custom logging, error tracking, or test assertions based on browser console output.
Code Reference
Source Location
packages/puppeteer-core/src/common/ConsoleMessage.ts
Signature
export interface ConsoleMessageLocation {
url?: string;
lineNumber?: number;
columnNumber?: number;
}
export type ConsoleMessageType =
| 'log' | 'debug' | 'info' | 'error' | 'warn'
| 'dir' | 'dirxml' | 'table' | 'trace' | 'clear'
| 'startGroup' | 'startGroupCollapsed' | 'endGroup'
| 'assert' | 'profile' | 'profileEnd' | 'count'
| 'timeEnd' | 'verbose';
export class ConsoleMessage {
constructor(
type: ConsoleMessageType,
text: string,
args: JSHandle[],
stackTraceLocations: ConsoleMessageLocation[],
frame?: Frame,
rawStackTrace?: Protocol.Runtime.StackTrace,
targetId?: string,
);
type(): ConsoleMessageType;
text(): string;
args(): JSHandle[];
location(): ConsoleMessageLocation;
stackTrace(): ConsoleMessageLocation[];
}
Import
import {ConsoleMessage, type ConsoleMessageLocation, type ConsoleMessageType} from 'puppeteer-core/lib/esm/puppeteer/common/ConsoleMessage.js';
I/O Contract
| Parameter | Type | Description |
|---|---|---|
| type | ConsoleMessageType |
The type of the console message (e.g., 'log', 'error') |
| text | string |
The text content of the console message |
| args | JSHandle[] |
An array of JSHandle arguments passed to the console call |
| stackTraceLocations | ConsoleMessageLocation[] |
Array of stack trace locations for the message |
| frame | Frame (optional) |
The frame from which the message originated |
| rawStackTrace | Protocol.Runtime.StackTrace (optional) |
The underlying CDP stack trace |
| targetId | string (optional) |
The target ID from which the message originated |
| Method | Return Type | Description |
|---|---|---|
type() |
ConsoleMessageType |
Returns the type of console message |
text() |
string |
Returns the text content |
args() |
JSHandle[] |
Returns the array of argument handles |
location() |
ConsoleMessageLocation |
Returns the first stack trace location or frame URL |
stackTrace() |
ConsoleMessageLocation[] |
Returns the full stack trace array |
Usage Examples
import puppeteer from 'puppeteer';
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Listen for console messages from the page
page.on('console', (msg) => {
console.log(`[${msg.type()}] ${msg.text()}`);
// Inspect message location
const location = msg.location();
if (location.url) {
console.log(` at ${location.url}:${location.lineNumber}:${location.columnNumber}`);
}
// Access underlying JSHandle arguments
for (const arg of msg.args()) {
console.log(` arg: ${arg}`);
}
});
// Trigger a console.log in the page
await page.evaluate(() => console.log('Hello from the page!'));
await browser.close();