Python的Playwright - 1.2 ConsoleMessage

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
"""
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值