Skip to main content
The ConsoleMessage class represents messages logged by the page to the browser console. Listen for console messages using the page.on('console') event.

Overview

Methods

type()

Get the type of the console message. Returns: string Possible values:
  • 'log'
  • 'debug'
  • 'info'
  • 'error'
  • 'warning'
  • 'dir'
  • 'dirxml'
  • 'table'
  • 'trace'
  • 'clear'
  • 'startGroup'
  • 'startGroupCollapsed'
  • 'endGroup'
  • 'assert'
  • 'profile'
  • 'profileEnd'
  • 'count'
  • 'timeEnd'

text()

Get the text of the console message. Returns: string

args()

Get the list of arguments passed to the console method. Returns: JSHandle[] Each argument is represented as a JSHandle that can be evaluated:

location()

Get the location in the source code where the console method was called. Returns: { url: string, lineNumber: number, columnNumber: number }
string
URL of the script where console was called
number
Line number in the script (0-based)
number
Column number in the script (0-based)

timestamp()

Get the timestamp when the message was logged. Returns: number Timestamp in milliseconds since Unix epoch:

page()

Get the page that emitted the console message. Returns: Page | null Returns null if the message came from a worker:

worker()

Get the worker that emitted the console message. Returns: Worker | null Returns null if the message came from a page:

Examples

Filter by Message Type

Log with Source Location

Inspect Complex Objects

Collect Console Timeline

Distinguish Page vs Worker

Best Practices

Always Check Message Type

Different message types may require different handling:

Handle Async Operations

If you need to evaluate JSHandles, make the listener async:

Dispose JSHandles

JSHandles keep objects in memory. Dispose them when done: