ConsoleMessage
ConsoleMessage 对象通过 page 的 page.on("console") 事件分派。页面中记录的每条控制台消息都会在 Playwright 上下文中触发相应的事件。
- 同步
- 异步
# Listen for all console logs
page.on("console", lambda msg: print(msg.text))
# Listen for all console events and handle errors
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
# Get the next console log
with page.expect_console_message() as msg_info:
# Issue console.log inside the page
page.evaluate("console.log('hello', 42, { foo: 'bar' })")
msg = msg_info.value
# Deconstruct print arguments
msg.args[0].json_value() # hello
msg.args[1].json_value() # 42
# Listen for all console logs
page.on("console", lambda msg: print(msg.text))
# Listen for all console events and handle errors
page.on("console", lambda msg: print(f"error: {msg.text}") if msg.type == "error" else None)
# Get the next console log
async with page.expect_console_message() as msg_info:
# Issue console.log inside the page
await page.evaluate("console.log('hello', 42, { foo: 'bar' })")
msg = await msg_info.value
# Deconstruct print arguments
await msg.args[0].json_value() # hello
await msg.args[1].json_value() # 42
属性
args
在 v1.9 之前添加传递给 console
函数调用的参数列表。另请参阅 page.on("console")。
用法
console_message.args
返回
location
在 v1.9 之前添加用法
console_message.location
返回
page
添加于: v1.34生成此控制台消息的页面,如果存在。
用法
console_message.page
返回
text
在 v1.9 之前添加控制台消息的文本。
用法
console_message.text
返回
type
在 v1.9 之前添加以下值之一: 'log'
, 'debug'
, 'info'
, 'error'
, 'warning'
, 'dir'
, 'dirxml'
, 'table'
, 'trace'
, 'clear'
, 'startGroup'
, 'startGroupCollapsed'
, 'endGroup'
, 'assert'
, 'profile'
, 'profileEnd'
, 'count'
, 'timeEnd'
。
用法
console_message.type
返回