selenium == 4.14.0 以下的就不支持以下设置方法 参见:详见
from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options import time import json # 设置 Chrome WebDriver 的路径 chrome_driver_path = r"E:\work\selenium\chromedriver.exe" # 请替换成你的 Chrome 驱动程序的路径 options = webdriver.ChromeOptions() path = Service(chrome_driver_path) options.set_capability('goog:loggingPrefs', {'performance': 'ALL'}) # 开启日志性能监听 driver = webdriver.Chrome(service=path, options=options) driver.get("https://www.baidu.com") time.sleep(3) performance_log = driver.get_log('performance') # 获取名称为 performance 的日志 for i in range(len(performance_log)): message = json.loads(performance_log[i]['message']) message = message['message']['params'] request = message.get('request') if(request is None): continue url = request.get('url') if(url == "https://www.baidu.com/"): # 通过requestId获取接口内容 detail_response = driver.execute_cdp_cmd('Network.getResponseBody', {'requestId': message['requestId']}) else: print("not:",url) print( detail_response)