繁体   English   中英

如何使用 Selenium 和 Python 绑定禁用日志记录

[英]How to disable logging using Selenium with Python binding

简单问题:如何在使用Python绑定的Selenium时完全禁用日志记录,ex代码如下:

browser = webdriver.Chrome()

我试过这样的事情:

options = webdriver.ChromeOptions();
options.add_argument('--log-level 3') 
browser = webdriver.Chrome(chrome_options=options)

甚至:

options = webdriver.ChromeOptions();
options.add_argument('--disable-logging') 
browser = webdriver.Chrome(chrome_options=options)

但文件“chromedriver.log”仍然出现在每次新的测试运行中。

您可以为 Chrome 浏览器设置options.add_argument("--log-level=3")以使用 Selenuim 运行,或者您可以将日志记录级别设置为更高的级别:

logger = logging.getLogger('selenium.webdriver.remote.remote_connection')
logger.setLevel(logging.WARNING)  # or any variant from ERROR, CRITICAL or NOTSET

但是在这种情况下无论如何都会出现一些消息,包括启动 DevTools 消息或 SSL 握手错误消息。

要以完全静默模式在控制台中使用 Selenium 运行 Chrome 浏览器,您应该使用以下代码段:

options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])

该技巧将抑制来自 Selenium 驱动程序或浏览器本身的任何控制台消息,包括DevTools listening on ws://127.0.0.1在一开始DevTools listening on ws://127.0.0.1的第一条消息。

同时,可以将一些运行时分步数据保存到服务日志文件中,以防已添加其参数。

driver = webdriver.Chrome(service_log_path='/dev/null')

Chrome 的 webdriver 的源代码显示存在一个名为service_log_path的选项。

因此,如果您想删除该文件,可以将此属性设置为

  • /dev/null如果您在 Linux/Unix 下运行;
  • 窗户下的NUL

希望能帮助到你

仅以 Windows 人员为例:

webdriver.Firefox(log_path='NUL')

接受的答案是正确的,但是如果您像我一样不熟悉 Python/Windows,这样的示例将减少您几个小时的谷歌时间。

要使用SeleniumPython禁用日志记录,您需要通过ChromeOptions()实例添加一个实验性选项,如下所示:

add_experimental_option('excludeSwitches', ['enable-logging'])

执行

兼容代码

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service

options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)

这对我有用:

chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

礼貌:

https://joshuatz.com/posts/2020/selenium-webdriver-disabling-chrome-logging-messages/

如果设置 service_log_path = None,则不会生成 geckodriver.log 文件:

driver = webdriver.Firefox(options=options, service_log_path=None)

我知道这已经过时了,但这仍然是您搜索一种防止从 selenium 进行日志记录的方法时出现的第一件事,并且它并没有为我摆脱“开发监听”消息,我找到了一种方法:

ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
IWebDriver driver = new ChromeDriver(service,options);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM