You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Selenium获取Chrome网络面板信息?遇性能日志报错

问题描述

Chrome版本111.0.5563.147、Selenium 4.8.2、Python 3.11环境下,调用driver.get_log('performance')时抛出错误:invalid argument: log type 'performance' not found.,当前使用的代码如下:

def get_driver():
    
    os.popen('chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\\selenum\\AutomationProfile"')
    global driver

    caps = DesiredCapabilities.CHROME
    caps['loggingPrefs'] = {'performance': 'ALL'}

    chrome_options = Options()
    chrome_options.add_argument('--enable-logging')
    chrome_options.set_capability('loggingPrefs', {'performance': 'ALL'})
    chrome_options.add_experimental_option("debuggerAddress","127.0.0.1:9222")
    chrome_options.set_capability("goog:chromeOptions", {"w3c": False})
    chrome_driver = "chromedriver.exe"
    #driver = webdriver.Chrome(chrome_driver, options=chrome_options, desired_capabilities=caps)
    driver = webdriver.Chrome(chrome_driver, desired_capabilities=caps, options=chrome_options)

    #print(driver.title)
    global wait
    wait = WebDriverWait(driver, 10, 0.2)
原因与解决办法
  • 问题根源有两个:一是Selenium 4+已经弃用旧的loggingPrefs配置方式,二是混用desired_capabilities和options导致配置冲突,再加上远程调试模式下的日志接口限制,才会出现日志类型找不到的错误。

  • 具体修复步骤:

    1. 删除DesiredCapabilities相关代码,Selenium 4之后所有配置都通过Options设置,不要混用两种配置方式。
    2. 将旧的loggingPrefs替换为goog:loggingPrefs,这是W3C规范下的正确键名,Selenium 4要求使用该键名。
    3. 启动Chrome时添加--enable-logging --v=1参数,确保日志功能正常开启;如果远程调试模式下仍无法获取日志,可以尝试直接由Selenium启动Chrome,不用远程调试,日志配置会更稳定。
  • 修复后的代码示例:

def get_driver():
    # 启动Chrome时添加日志相关参数
    os.popen('chrome.exe --remote-debugging-port=9222 --user-data-dir="C:\\selenum\\AutomationProfile" --enable-logging --v=1')
    
    global driver, wait
    chrome_options = Options()
    chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
    # Selenium 4+用goog:loggingPrefs配置性能日志
    chrome_options.set_capability("goog:loggingPrefs", {"performance": "ALL"})
    
    chrome_driver = "chromedriver.exe"
    driver = webdriver.Chrome(chrome_driver, options=chrome_options)
    
    wait = WebDriverWait(driver, 10, 0.2)
  • 额外提醒:
    务必确保ChromeDriver版本与Chrome版本完全匹配(Chrome 111对应ChromeDriver 111.x),版本不匹配也会引发各类接口异常。

内容的提问来源于stack exchange,提问作者chenxxsust

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 15:08:25