Skip to main content

ConsoleMessage

ConsoleMessage objects are dispatched by page via the page.on('console') event. For each console message logged in the page there will be corresponding event in the Playwright context.

// Listen for all console logs
page.on('console', msg => console.log(msg.text()));

// Listen for all console events and handle errors
page.on('console', msg => {
if (msg.type() === 'error')
console.log(`Error text: "${msg.text()}"`);
});

// Get the next console log
const msgPromise = page.waitForEvent('console');
await page.evaluate(() => {
console.log('hello', 42, { foo: 'bar' }); // Issue console.log inside the page
});
const msg = await msgPromise;

// Deconstruct console log arguments
await msg.args()[0].jsonValue(); // hello
await msg.args()[1].jsonValue(); // 42

方法 (Methods)

args

Added before v1.9 consoleMessage.args

List of arguments passed to a console function call. See also page.on('console').

使用方式

consoleMessage.args();

傳回值


location

Added before v1.9 consoleMessage.location

使用方式

consoleMessage.location();

傳回值

  • Object#
    • url string

      URL of the resource.

    • lineNumber number

      0-based line number in the resource.

    • columnNumber number

      0-based column number in the resource.


page

Added in: v1.34 consoleMessage.page

The page that produced this console message, if any.

使用方式

consoleMessage.page();

傳回值


text

Added before v1.9 consoleMessage.text

The text of the console message.

使用方式

consoleMessage.text();

傳回值


type

Added before v1.9 consoleMessage.type

使用方式

consoleMessage.type();

傳回值

  • "log" | "debug" | "info" | "error" | "warning" | "dir" | "dirxml" | "table" | "trace" | "clear" | "startGroup" | "startGroupCollapsed" | "endGroup" | "assert" | "profile" | "profileEnd" | "count" | "timeEnd"#