跳转到主要内容

ConsoleMessage

ConsoleMessage 对象通过页面的 page.on('console') 事件进行分发。页面中记录的每个控制台消息都将在 Playwright 上下文中有一个相应的事件。

// 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

方法

args

v1.9 之前添加 consoleMessage.args

传递给 console 函数调用的参数列表。另请参阅 page.on('console')

用法

consoleMessage.args();

返回


location

v1.9 之前添加 consoleMessage.location

用法

consoleMessage.location();

返回

  • Object#
    • url string

      资源的 URL。

    • lineNumber number

      资源中从 0 开始的行号。

    • columnNumber number

      资源中从 0 开始的列号。


page

新增于: v1.34 consoleMessage.page

生成此控制台消息的页面(如果有)。

用法

consoleMessage.page();

返回


text

v1.9 之前添加 consoleMessage.text

控制台消息的文本。

用法

consoleMessage.text();

返回


type

v1.9 之前添加 consoleMessage.type

用法

consoleMessage.type();

返回

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