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
方法
🌐 Methods
Args
Added before v1.9传递给 console 函数调用的参数列表。另请参见 Page.Console。
🌐 List of arguments passed to a console function call. See also Page.Console.
用法
ConsoleMessage.Args
返回
Location
Added before v1.9资源的 URL,后跟资源中以 0 为起点的行号和列号,格式为 URL:line:column。
🌐 URL of the resource followed by 0-based line and column numbers in the resource formatted as URL:line:column.
用法
ConsoleMessage.Location
返回
Page
Added in: v1.34生成此控制台消息的页面(如果有)。
🌐 The page that produced this console message, if any.
用法
ConsoleMessage.Page
返回
Text
Added before v1.9控制台消息的文本。
🌐 The text of the console message.
用法
ConsoleMessage.Text
返回
Type
Added before v1.9以下值之一:'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'.
用法
ConsoleMessage.Type
返回
Worker
Added in: v1.57产生此控制台消息的网页工作者或服务工作者(如果有的话)。请注意,来自网页工作者的控制台消息也有非空的 ConsoleMessage.Page。
🌐 The web worker or service worker that produced this console message, if any. Note that console messages from web workers also have non-null ConsoleMessage.Page.
用法
ConsoleMessage.Worker
返回