如何抑制Selenium控制台输出?Python脚本优化求助
问题描述
我编写了一个Python脚本,可选择不同歌单并在浏览器中打开YouTube播放。但每次运行时控制台会产生大量消息,我希望能屏蔽这些消息(优先屏蔽Selenium相关,屏蔽所有也可)。我尝试了以下方法但均无效:
- 添加ChromeOptions参数:
options = webdriver.ChromeOptions() options.add_argument("--log-level=OFF") - 原本有
options.add_experimental_option('excludeSwitches', ['enable-logging'])这行代码,删除后无变化; - 设置driver参数:
driver = webdriver.Chrome(service_log_path=os.devnull) driver = webdriver.Chrome(service_log_path='NUL')
控制台仍输出如下内容:
DevTools listening on ws://127.0.0.1:55146/devtools/browser/9ffb59e1-adc0-480e-835e-e36ffe7cb428 DevTools listening on ws://127.0.0.1:55156/devtools/browser/5b6b567e-6ae1-4cdd-8206-915e788e6fe0 [8812:12068:0207/132732.310:ERROR:device_event_log_impl.cc(215)] [13:27:32.309] USB: usb_service_win.cc:415 Could not read device interface GUIDs: The system cannot find the file specified. (0x2) [8812:12068:0207/132732.310:ERROR:device_event_log_impl.cc(215)] [13:27:32.310] USB: usb_device_handle_win.cc:1046 Failed to read descriptor from node connection: A device attached to the system is not functioning. (0x1F) Traceback (most recent call last): File "c:\Users\EliBa\OneDrive\Documents\Coding Projects\Python\musico.py", line 14, in <module> driver.fullscreen_window() File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 596, in fullscreen_window self.execute(Command.FULLSCREEN_WINDOW) File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 440, in execute self.error_handler.check_response(response) File "C:\Users\EliBa\AppData\Local\Programs\Python\Python311\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 245, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchWindowException: Message: no such window: target window already closed from unknown error: web view not found (Session info: chrome=109.0.5414.120) Stacktrace: Backtrace: (No symbol) [0x002B6643] (No symbol) [0x0024BE21] (No symbol) [0x0014DA9D] (No symbol) [0x0012EF6A] (No symbol) [0x001A3AAB] (No symbol) [0x001B61B6] (No symbol) [0x0019FB76] (No symbol) [0x001749C1] (No symbol) [0x00175E5D] GetHandleVerifier [0x0052A142+2497106] GetHandleVerifier [0x005585D3+2686691] GetHandleVerifier [0x0055BB9C+2700460] GetHandleVerifier [0x00363B10+635936] (No symbol) [0x00254A1F] (No symbol) [0x0025A418] (No symbol) [0x0025A505] (No symbol) [0x0026508B] BaseThreadInitThunk [0x76CC7D69+25] RtlInitializeExceptionChain [0x77E2BB9B+107] RtlClearBits [0x77E2BB1F+191] (No symbol) [0x00000000]
解决方案
1. 优化ChromeOptions配置,屏蔽浏览器端日志
补充更多参数,全面抑制Chrome的各类日志输出:
options = webdriver.ChromeOptions() # 仅保留严重错误日志 options.add_argument("--log-level=3") # 静默模式,屏蔽所有非必要输出 options.add_argument("--silent") options.add_argument("--disable-logging") # 关闭日志开关和自动化提示 options.add_experimental_option('excludeSwitches', ['enable-logging', 'enable-automation']) # 禁用自动化扩展 options.add_experimental_option('useAutomationExtension', False)
2. 通过Service类配置Selenium日志路径(Selenium 4+)
Selenium 4之后需要通过Service类来指定日志路径,而非直接在driver构造函数中传入:
from selenium.webdriver.chrome.service import Service import os # 将日志导向空设备,Windows可用'NUL'替代os.devnull service = Service(service_log_path=os.devnull) driver = webdriver.Chrome(options=options, service=service)
3. 捕获特定异常,避免栈信息输出
针对脚本中出现的NoSuchWindowException,用try-except块包裹相关代码,阻止异常栈打印:
import selenium.common.exceptions try: driver.fullscreen_window() except selenium.common.exceptions.NoSuchWindowException: pass # 可根据需求替换为自定义处理逻辑
4. 重定向Python全局输出流(终极方案)
如果上述方法仍有遗漏日志,可直接在脚本开头将标准输出和错误输出重定向到空设备,彻底屏蔽所有控制台信息:
import sys import os # 重定向stdout和stderr到空设备 sys.stdout = open(os.devnull, 'w') sys.stderr = open(os.devnull, 'w')
注意:该方法会屏蔽所有输出,包括你自己添加的调试打印,需谨慎使用。
内容的提问来源于stack exchange,提问作者Eli Bauer
相关产品推荐
相关产品推荐

