如何在Selenium-Wire中禁用Edge WebDriver的控制台日志输出
如何在Selenium-Wire中禁用Edge WebDriver的控制台日志输出
我完全懂这种被无关日志刷屏的烦躁!尤其是CLI工具,干净的输出体验太重要了。你试过的--log-level=3、excludeSwitches这些都是Selenium的常规操作,但因为用了Selenium-Wire这个封装库,它本身也有独立的日志系统,所以需要多做几步针对性配置,我来给你几个可行的组合方案:
方案一:同时屏蔽Selenium-Wire和Edge Driver的日志
Selenium-Wire会输出自己的请求捕获日志,加上Edge内核的原生日志,我们需要分别处理:
import logging from seleniumwire import webdriver # 彻底禁用Selenium-Wire的所有日志输出 logging.getLogger('seleniumwire').setLevel(logging.NOTSET) options = webdriver.EdgeOptions() options.add_argument("--headless") # 屏蔽Edge的初始化日志 options.add_experimental_option('excludeSwitches', ['enable-logging']) # 设置Edge日志级别为仅显示严重错误 options.add_argument('--log-level=3') # 额外添加静默参数强化屏蔽 options.add_argument('--silent') driver = webdriver.Edge(options=options)
方案二:结合Edge Service的日志重定向
如果还是有Chromium内核的错误日志(比如你提到的fallback_task_provider.cc错误),可以通过Edge Service把驱动的日志直接导向空设备,再配合Selenium-Wire的日志屏蔽:
import logging from seleniumwire import webdriver from selenium.webdriver.edge.service import Service # 将Selenium-Wire日志级别设为仅警告及以上(避免无关调试信息) logging.getLogger('seleniumwire').setLevel(logging.WARNING) options = webdriver.EdgeOptions() options.add_argument("--headless") options.add_experimental_option('excludeSwitches', ['enable-logging']) options.add_argument('--log-level=3') options.add_argument('--disable-gpu') # 把EdgeDriver的所有日志输出到nul(Windows),Linux/macOS换成/dev/null service = Service(log_output="nul", service_args=["--log-level=OFF"]) # 初始化驱动时传入配置好的Service driver = webdriver.Edge(options=options, service=service)
方案三:极端情况的兜底方案(慎用)
如果上述方法还残留少量内核日志,你可以临时重定向Python的标准错误输出,但这会屏蔽所有程序的stderr信息,可能错过自己代码的报错,所以只推荐作为最后手段:
import sys import os from seleniumwire import webdriver # 重定向stderr到空设备,Windows用"nul",Linux/macOS用"/dev/null" sys.stderr = open(os.devnull, 'w') options = webdriver.EdgeOptions() options.add_argument("--headless") options.add_experimental_option('excludeSwitches', ['enable-logging']) driver = webdriver.Edge(options=options)
为什么之前的方法没用?
你之前尝试的选项主要针对原生Selenium的Edge Driver,但Selenium-Wire作为上层封装,会额外输出请求拦截相关的日志;而且Chromium内核的部分底层错误日志,需要通过Service的日志重定向或者更严格的命令行参数才能屏蔽——这些日志虽然标了ERROR,但确实不影响驱动功能,属于Chromium的内部调试信息。
备注:内容来源于stack exchange,提问作者Mews
相关产品推荐
相关产品推荐

