如何在运行Selenium WebDriver时抑制Chrome的GCM DEPRECATED_ENDPOINT错误日志?
如何在运行Selenium WebDriver时抑制Chrome的GCM DEPRECATED_ENDPOINT错误日志?
我太懂这种被无关日志刷屏的烦躁了!你遇到的这些GCM错误、语音转录注册提示还有TensorFlow相关警告,全是Chrome后台那些冗余服务在捣乱,和你的爬虫逻辑完全不沾边。你已经加了不少抑制参数,但还差几个关键设置,试试下面的方法:
1. 新增ChromeOptions参数,直接禁用冗余服务
Chrome的GCM(Google Cloud Messaging)服务已经被废弃,但它还会在后台自动尝试注册,导致那些DEPRECATED_ENDPOINT错误。直接禁用GCM相关功能,同时关掉语音转录、TensorFlow Lite这些你用不上的服务,就能干掉大部分日志:
# 禁用GCM及谷歌云消息服务 options.add_argument('--disable-gcm') options.add_argument('--disable-google-cloud-messaging') # 禁用语音转录功能(解决VoiceTranscriptionCapability日志) options.add_argument('--disable-features=VoiceTranscription') # 禁用TensorFlow Lite相关组件(解决XNNPACK和动态张量警告) options.add_argument('--disable-features=TensorFlowLiteCpuDelegate,Xnnpack')
2. 进一步抑制Chrome的底层日志输出
有时候Chrome的日志会直接绕过Selenium的设置,输出到系统的stderr/stdout。你可以通过环境变量限制Chrome的日志级别,或者直接让ChromeDriver不生成日志文件:
import os # 设置环境变量,让Chrome只输出严重致命错误(3=FATAL级别,0-4对应从详细到致命) os.environ['CHROME_LOG_LEVEL'] = '3' # 初始化ChromeDriver服务时,将日志导向空设备(Windows用NUL,Linux/macOS用/dev/null) from selenium.webdriver.chrome.service import Service service = Service( executable_path='chromedriver.exe', # 替换成你的chromedriver实际路径 log_path='NUL' if os.name == 'nt' else '/dev/null' )
3. 过滤Selenium的日志输出
如果还有漏网的日志,可以通过Python的日志系统过滤掉特定关键词的内容:
import logging from selenium.webdriver.remote.remote_connection import LOGGER # 降低Selenium的全局日志级别到WARNING,只输出重要提示 LOGGER.setLevel(logging.WARNING) # 自定义过滤器,精准过滤掉你不想看到的日志关键词 class FilterIrrelevantLogs(logging.Filter): def filter(self, record): # 过滤包含这些关键词的日志 excluded_keywords = [ 'DEPRECATED_ENDPOINT', 'PHONE_REGISTRATION_ERROR', 'VoiceTranscriptionCapability', 'TensorFlow Lite' ] return not any(keyword in record.getMessage() for keyword in excluded_keywords) # 给Selenium的日志处理器加上过滤器 for handler in LOGGER.handlers: handler.addFilter(FilterIrrelevantLogs())
完整的配置示例
把所有优化整合到你的代码里,最终的ChromeOptions和Driver初始化代码大概是这样:
from selenium import webdriver from selenium.webdriver.chrome.service import Service import os # 先设置Chrome日志级别 os.environ['CHROME_LOG_LEVEL'] = '3' options = webdriver.ChromeOptions() # 你的原有设置 options.add_argument('--headless=new') options.add_argument('--no-sandbox') options.add_argument('--disable-dev-shm-usage') options.add_argument('--disable-gpu') options.add_argument('--mute-audio') options.add_argument('--metrics-recording-only') options.add_argument('--disable-extensions') options.add_argument('--disable-infobars') options.add_argument('--disable-notifications') options.add_argument('--disable-cloud-import') options.add_argument('--disable-sync') options.add_argument('--disable-client-side-phishing-detection') options.add_argument('--disable-background-networking') options.add_argument('--disable-background-timer-throttling') options.add_argument('--disable-backgrounding-occluded-windows') options.add_argument('--disable-component-update') options.add_argument('--disable-default-apps') options.add_argument('--no-first-run') options.add_argument('--no-default-browser-check') options.add_argument('--ignore-certificate-errors') options.add_argument('--guest') options.add_argument( 'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' 'AppleWebKit/537.36 (KHTML, like Gecko) ' 'Chrome/98.0.4758.102 Safari/537.36' ) options.add_experimental_option('excludeSwitches', ['enable-logging']) # 新增的抑制日志参数 options.add_argument('--disable-gcm') options.add_argument('--disable-google-cloud-messaging') options.add_argument('--disable-features=VoiceTranscription,TensorFlowLiteCpuDelegate,Xnnpack') # 初始化服务和Driver service = Service( executable_path='chromedriver.exe', log_path='NUL' if os.name == 'nt' else '/dev/null' ) driver = webdriver.Chrome(service=service, options=options)
这些设置应该能把那些烦人的冗余日志全部干掉,让你专注于爬虫本身的输出。
内容来源于stack exchange




