ConsoleMessage
ConsoleMessage 对象由页面通过 Page.Console 事件派发。页面中记录的每条控制台消息,在 Playwright 上下文中都会有对应的事件。
// 监听所有控制台消息并将它们打印到标准输出。
page.Console += (_, msg) => Console.WriteLine(msg.Text);
// 监听所有控制台消息并将错误打印到标准输出。
page.Console += (_, msg) =>
{
if ("error".Equals(msg.Type))
Console.WriteLine("Error text: " + msg.Text);
};
// 获取下一条控制台消息
var waitForMessageTask = page.WaitForConsoleMessageAsync();
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
var message = await waitForMessageTask;
// 解构 console.log 参数
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
方法
Args
在 v1.9 之前添加传递给 console
函数调用的参数列表。另请参阅 Page.Console。
用法
ConsoleMessage.Args
返回值
Location
在 v1.9 之前添加资源的 URL,后跟资源中从 0 开始的行号和列号,格式为 URL:行:列
。
用法
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
返回值