ConsoleMessage
ConsoleMessage 对象通过 page 的 Page.onConsoleMessage(handler) 事件分发。页面中记录的每条控制台消息都会在 Playwright 上下文中触发相应的事件。
// Listen for all console messages and print them to the standard output.
page.onConsoleMessage(msg -> System.out.println(msg.text()));
// Listen for all console messages and print errors to the standard output.
page.onConsoleMessage(msg -> {
if ("error".equals(msg.type()))
System.out.println("Error text: " + msg.text());
});
// Get the next console message
ConsoleMessage msg = page.waitForConsoleMessage(() -> {
// Issue console.log inside the page
page.evaluate("console.log('hello', 42, { foo: 'bar' });");
});
// Deconstruct console.log arguments
msg.args().get(0).jsonValue(); // hello
msg.args().get(1).jsonValue(); // 42
方法
args
添加于 v1.9 版本之前传递给 console
函数调用的参数列表。另请参阅 Page.onConsoleMessage(handler)。
用法
ConsoleMessage.args();
返回值
location
添加于 v1.9 版本之前资源的 URL,后跟资源中基于 0 的行号和列号,格式为 URL:line:column
。
用法
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();
返回值