Skip to main content

ConsoleMessage

ConsoleMessage objects are dispatched by page via the event Page.Console 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.Console += (_, msg) => Console.WriteLine(msg.Text);

// Listen for all console events and handle errors
page.Console += (_, msg) =>
{
if ("error".Equals(msg.Type))
Console.WriteLine("Error text: " + msg.Text);
};

// Get the next System.out.println
var waitForMessageTask = page.WaitForConsoleMessageAsync();
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
var message = await waitForMessageTask;
// Deconstruct console.log arguments
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42

ConsoleMessage.Args

Added in: v1.8

List of arguments passed to a console function call. See also event Page.Console.

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'.