我在 Windows 上使用 selenium 进行抓取脚本以及 chromedriver 服务,这里是如何初始化它们的
# Setting up the options
options = Options()
options.add_argument("--headless=new")
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors=yes')
# Setting up service
service = Service(ChromeDriverManager().install(), log_output='nul')
driver = webdriver.Chrome(service=service,options=options)
# Rest of the code ...
我的脚本工作正常,我只是想忽略这些恼人的错误/警告,这些错误/警告在我运行它时不断出现在控制台中,
DevTools listening on ws://127.0.0.1:57253/devtools/browser/10819d8d-19ce-4893-8632-7c841dc8f6f7
[12856:28660:0307/001815.740:ERROR:socket_manager.cc(141)] Failed to resolve address for stun.l.google.com., errorcode: -105
ERROR: Time is after notAfter
[12856:28660:0307/001818.072:ERROR:socket_manager.cc(141)] Failed to resolve address for stun.l.google.com., errorcode: -105
我想知道是否有办法做到这一点......
我尝试将此参数添加到 Service 构造函数中,但没有修复它:
log_output='nul'
我也尝试过,但没有成功:
# Suppressing stderr
sys.stderr = open(os.devnull, 'w')
# Setting up the options
options = Options()
options.add_argument('--ignore-ssl-errors=yes')
options.add_argument('--ignore-certificate-errors')
# Instantiate the WebDriver with suppressed console errors
service = Service(ChromeDriverManager().install())
driver = webdriver.Chrome(service=service, options=options)
sys.stderr.close()
sys.stderr = sys.__stderr__
# Rest of the code
添加此行:
chrome_options.add_argument("--log-level=3")
0: verbose
1: info
2: warning
3: error