ConsoleMessage
ConsoleMessage 对象由页面通过 Page.Console 事件分发。对于页面中记录的每个控制台消息,Playwright 上下文中都会有相应的事件。
// Listen for all console messages and print them to the standard output.
page.Console += (_, msg) => Console.WriteLine(msg.Text);
// Listen for all console messages and print errors to the standard output.
page.Console += (_, msg) =>
{
if ("error".Equals(msg.Type))
Console.WriteLine("Error text: " + msg.Text);
};
// Get the next console message
var waitForMessageTask = page.WaitForConsoleMessageAsync();
await page.EvaluateAsync("console.log('hello', 42, { foo: 'bar' });");
var message = await waitForMessageTask;
// Deconstruct console.log arguments
await message.Args.ElementAt(0).JsonValueAsync<string>(); // hello
await message.Args.ElementAt(1).JsonValueAsync<int>(); // 42
方法
参数
Added before v1.9传递给console
函数调用的参数列表。另请参阅Page.Console。
用法
ConsoleMessage.Args
返回
位置
Added before v1.9资源的URL,后跟资源中基于0的行号和列号,格式为URL:line:column
。
用法
ConsoleMessage.Location
返回
页面
Added in: v1.34生成此控制台消息的页面(如果有的话)。
用法
ConsoleMessage.Page
返回
文本
Added before v1.9控制台消息的文本。
用法
ConsoleMessage.Text
返回
类型
Added before v1.9以下值之一:'log'
, 'debug'
, 'info'
, 'error'
, 'warning'
, 'dir'
, 'dirxml'
, 'table'
, 'trace'
, 'clear'
, 'startGroup'
, 'startGroupCollapsed'
, 'endGroup'
, 'assert'
, 'profile'
, 'profileEnd'
, 'count'
, 'timeEnd'
.
用法
ConsoleMessage.Type
返回