Selenium 4.10配置Chrome性能日志采集的问题
在Selenium 4.10中配置Chrome驱动收集性能日志的正确方法
Selenium 4.10已移除webdriver.Chrome的desired_capabilities参数,需通过ChromeOptions直接配置日志偏好,替代旧版DesiredCapabilities的用法。以下是修复后的完整代码:
def get_driver(self): opts = webdriver.ChromeOptions() opts.add_argument("--disable-blink-features=AutomationControlled") opts.add_argument("--ignore-certificate-errors") opts.add_argument( f"--user-agent=Lynx: Lynx/2.8.8pre.4 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/2.12.23" ) opts.page_load_strategy = "normal" # 核心改动:用ChromeOptions设置性能日志收集偏好 opts.set_capability("goog:loggingPrefs", {"performance": "ALL"}) service = Service( service_args=["--log-level=ALL", "--append-log"], log_path="LOGCHROME.txt", ) driver = webdriver.Chrome(service=service, options=opts) driver.execute_cdp_cmd("Performance.enable", {}) return driver
关键改动说明
- 替代DesiredCapabilities:通过
ChromeOptions.set_capability()方法直接设置goog:loggingPrefs,这是Selenium 4+官方推荐的配置方式,原desired_capabilities参数已被废弃。 - 保留CDP启用命令:
driver.execute_cdp_cmd("Performance.enable", {})仍需保留,确保Chrome的性能监控功能被激活。 - 内置驱动管理:Selenium 4.10自带驱动自动匹配功能,无需依赖
webdriver_manager,解决了之前驱动版本识别为win32的bug。
验证日志收集
在驱动操作完成后,可通过以下代码获取性能日志:
performance_logs = driver.get_log('performance') for log in performance_logs: print(log)
内容的提问来源于stack exchange,提问作者Evan
相关产品推荐
相关产品推荐

