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 之前新增傳遞到 console
函數呼叫的引數列表。另請參閱 page.on('console')。
用法
consoleMessage.args();
回傳
location
在 v1.9 之前新增用法
consoleMessage.location();
回傳
page
在 v1.34 中新增產生此主控台訊息的頁面(如果有的話)。
用法
consoleMessage.page();
回傳
text
在 v1.9 之前新增主控台訊息的文字。
用法
consoleMessage.text();
回傳
type
在 v1.9 之前新增下列值之一:'log'
、'debug'
、'info'
、'error'
、'warning'
、'dir'
、'dirxml'
、'table'
、'trace'
、'clear'
、'startGroup'
、'startGroupCollapsed'
、'endGroup'
、'assert'
、'profile'
、'profileEnd'
、'count'
、'timeEnd'
。
用法
consoleMessage.type();
回傳