ConsoleMessage
ConsoleMessage 对象通过页面的 page.on("console") 事件分发。页面中记录的每条控制台消息在 Playwright 上下文中都会有对应的事件。
- Sync
- Async
# 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
属性
🌐 Properties
args
Added before v1.9传递给 console 函数调用的参数列表。另请参见 page.on("console")。
🌐 List of arguments passed to a console function call. See also page.on("console").
用法
console_message.args
返回
location
Added before v1.9用法
console_message.location
返回
page
Added in: v1.34生成此控制台消息的页面(如果有)。
🌐 The page that produced this console message, if any.
用法
console_message.page
返回
text
Added before v1.9控制台消息的文本。
🌐 The text of the console message.
用法
console_message.text
返回
type
Added before v1.9用法
console_message.type
返回
- "日志" | "调试" | "信息" | "错误" | "警告" | "目录" | "目录XML" | "表格" | "跟踪" | "清除" | "开始分组" | "开始折叠分组" | "结束分组" | "断言" | "分析" | "分析结束" | "计数" | "计时" | "计时结束"#
worker
Added in: v1.57产生此控制台消息的网页工作者或服务工作者(如果有的话)。请注意,来自网页工作者的控制台消息也有非空的 console_message.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 console_message.page.
用法
console_message.worker
返回