ConsoleMessage
ConsoleMessage 对象通过 page.on('console') 事件由页面分发。对于页面中记录的每条控制台消息,Playwright 上下文中都会有相应的事件。
// 监听所有控制台日志
page.on('console', msg => console.log(msg.text()))
// 监听所有控制台事件并处理错误
page.on('console', msg => {
if (msg.type() === 'error')
console.log(`Error text: "${msg.text()}"`);
});
// 获取下一个控制台日志
const [msg] = await Promise.all([
page.waitForEvent('console'),
// 在页面内触发 console.log
page.evaluate(() => {
console.log('hello', 42, { foo: 'bar' });
}),
]);
// 解构控制台日志参数
await msg.args[0].jsonValue() // hello
await msg.args[1].jsonValue() // 42
consoleMessage.args()
Added in: v1.8传递给 console 函数调用的参数列表。另请参阅 page.on('console')。
consoleMessage.location()
Added in: v1.8consoleMessage.text()
Added in: v1.8控制台消息的文本。
consoleMessage.type()
Added in: v1.8以下值之一:'log'、'debug'、'info'、'error'、'warning'、'dir'、'dirxml'、'table'、'trace'、'clear'、'startGroup'、'startGroupCollapsed'、'endGroup'、'assert'、'profile'、'profileEnd'、'count'、'timeEnd'。