Skip to main content

ConsoleMessage

ConsoleMessage objects are dispatched by page via the Page.onConsoleMessage(handler) event. For each console messages logged in the page there will be corresponding event in the Playwright context.

// Listen for all System.out.printlns
page.onConsoleMessage(msg -> System.out.println(msg.text()));

// Listen for all console events and handle errors
page.onConsoleMessage(msg -> {
if ("error".equals(msg.type()))
System.out.println("Error text: " + msg.text());
});

// Get the next System.out.println
ConsoleMessage msg = page.waitForConsoleMessage(() -> {
// Issue console.log inside the page
page.evaluate("console.log('hello', 42, { foo: 'bar' });");
});

// Deconstruct console.log arguments
msg.args().get(0).jsonValue() // hello
msg.args().get(1).jsonValue() // 42

ConsoleMessage.args()

Added in: v1.8

List of arguments passed to a console function call. See also Page.onConsoleMessage(handler).

ConsoleMessage.location()

Added in: v1.8

URL of the resource followed by 0-based line and column numbers in the resource formatted as URL:line:column.

ConsoleMessage.text()

Added in: v1.8

The text of the console message.

ConsoleMessage.type()

Added in: v1.8

One of the following values: 'log', 'debug', 'info', 'error', 'warning', 'dir', 'dirxml', 'table', 'trace', 'clear', 'startGroup', 'startGroupCollapsed', 'endGroup', 'assert', 'profile', 'profileEnd', 'count', 'timeEnd'.