如何在 Python 中使用 Chrome 关闭 Selenium 中的命令行日志记录

use*_*087 5 python windows selenium command-line

我正在尝试使用 Selenium 运行 Python 脚本,虽然一切正常,但我个人的 print() 到控制台行隐藏在大量 Selenium/Chromedriver 输出之间,如下所示:

1128/150256.806:INFO:CONSOLE(0)] "The SSL certificate used to load resources from [some-link.com] will be distrusted in the future. Once distrusted, users will be prevented from loading these resources. See https://g.co/chrome/symantecpkicerts for more information.", source: [current-page.com] (0)
Run Code Online (Sandbox Code Playgroud)

我检查了这些链接是什么,它们只是我正在查看的页面上的广告,因此完全没有用。此外,由于每次页面加载/重新加载时都会随机生成广告,因此链接不同,因此输出永无止境。这非常烦人,并且很难看到我的程序中实际发生了什么。有什么办法可以用一些 Selenium 选项或其他东西来关闭它吗?

奇怪的是,使用 PyDev 在 Eclipse Oxygen 中运行程序根本不显示任何 Selenium 的输出,只有当我使用命令行运行它时。

编辑:按照提到的可能重复项的说明没有帮助。我尝试将日志记录级别设置为最高、CRITICAL,但上面提到的输出仍然通过并淹没了控制台。

小智 5

解决此问题的最佳方法是在驱动程序中添加 --log-level 选项。这看起来像这样:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--log-level=3")
driver = webdriver.Chrome(chrome_options=chrome_options)
Run Code Online (Sandbox Code Playgroud)