Skip to main content

ConsoleMessage

ConsoleMessage 对象通过 page.on('console') 事件按页分派。对于页面中记录的每条控制台消息,Playwright 上下文中都会有相应的事件。

¥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

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

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

用法

¥Usage

consoleMessage.args();

返回

¥Returns


location

Added before v1.9 consoleMessage.location

用法

¥Usage

consoleMessage.location();

返回

¥Returns

资源的 URL。

¥URL of the resource.

资源中从 0 开始的行号。

¥0-based line number in the resource.

资源中从 0 开始的列号。

¥0-based column number in the resource.


page

Added in: v1.34 consoleMessage.page

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

¥The page that produced this console message, if any.

用法

¥Usage

consoleMessage.page();

返回

¥Returns


text

Added before v1.9 consoleMessage.text

控制台消息的文本。

¥The text of the console message.

用法

¥Usage

consoleMessage.text();

返回

¥Returns


type

Added before v1.9 consoleMessage.type

以下值之一:'log''debug''info''error''warning''dir''dirxml''table''trace''clear''startGroup''startGroupCollapsed''endGroup''assert''profile''profileEnd''count''timeEnd'

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

用法

¥Usage

consoleMessage.type();

返回

¥Returns