如何通过Selenium Python使用CDP命令收集性能跟踪?

0 投票
1 回答
50 浏览
提问于 2025-04-13 20:58

我想通过Selenium和Python收集Chrome的性能分析数据,类似于这个Java的方法

合适的CDP命令是:

Tracing.start
Tracing.end
Tracing.dataCollected

我看到了一些关于BiDi Api的例子,并尝试使用它们,但在Python中无法实现我的需求。

请帮我提供一个例子,说明如何异步监听Tracing.dataCollected事件,并通过Selenium-Python收集性能分析数据。

1 个回答

0

你可以通过SeleniumBase来获取Python/Selenium的性能日志:https://github.com/seleniumbase/SeleniumBase - (使用pip install seleniumbase来安装)

下面是一个示例,展示了如何在绕过验证码的情况下获取性能日志:

from rich.pretty import pprint
from seleniumbase import Driver

driver = Driver(uc=True, log_cdp=True)
try:
    driver.uc_open_with_reconnect("https://seleniumbase.io/apps/turnstile")
    driver.uc_switch_to_frame("iframe")
    driver.uc_click("span.mark")
    driver.sleep(3)
    pprint(driver.get_log("performance"))
finally:
    driver.quit()

撰写回答