ConsoleMessage
对象通过page.on(“console”)事件按页面调度
常见的几种属性:
- console_message.args
- console_message.location
- console_message.text
- console_message.type
以请求百度为例:
from playwright.sync_api import sync_playwright
with sync_playwright() as sp:
browser = sp.chromium.launch(headless=False)
context = browser.new_context()
page = context.new_page()
# 代码实现
page.on('console', lambda console: print(console.args))
page.goto("http://www.baidu.com")
context.close()
browser.close()
Console输出:
记录各个属性的输出:
"""
console.args(List[JSHandle]):
# [<JSHandle preview=每一个星球都有一个驱动核心,
# 每一种思想都有影响力的种子。
# 感受世界的温度,
# 年轻的你也能成为改变世界的动力,
# 百度珍惜你所有的潜力。
# 你的潜力,是改变世界的动力!
#
# >]
# [<JSHandle preview=%c百度2022校园招聘简历投递:https://talent.baidu.com/external/baidu/campus.html>, <JSHandle preview=color:red>]
console.location(Dict, 上面红色框框):
# {'url': 'https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_bd9cfcb.js', 'lineNumber': 233, 'columnNumber': 547}
# {'url': 'https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/global/js/all_async_search_bd9cfcb.js', 'lineNumber': 233, 'columnNumber': 654}
console.text(str):
# 每一个星球都有一个驱动核心,
# 每一种思想都有影响力的种子。
# 感受世界的温度,
# 年轻的你也能成为改变世界的动力,
# 百度珍惜你所有的潜力。
# 你的潜力,是改变世界的动力!
# %c百度2022校园招聘简历投递:https://talent.baidu.com/external/baidu/campus.html color:red
console.type(下列值的其中一个:'log', 'debug', 'info', 'error', 'warning', 'dir', 'dirxml', 'table', 'trace', 'clear', 'startGroup', 'startGroupCollapsed', 'endGroup', 'assert', 'profile', 'profileEnd', 'count', 'timeEnd'):
# log
"""