如何关闭 selenium 3 中的 Marionette/gecko 驱动程序日志

How to turn off the Marionette/gecko driver logs in selenium 3

我需要关闭 Marionette/GeckoDriver 日志;有什么办法吗?我一直在寻找很多,但我没有得到正确的答案。 INFO 日志是:

 1484653905833  geckodriver INFO    Listening on 127.0.0.1:15106
    Jan 17, 2017 5:21:46 PM org.openqa.selenium.remote.ProtocolHandshake createSession
    INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
    1484653906715   mozprofile::profile INFO    Using profile path C:\Users\vtiger\AppData\Local\Temp\rust_mozprofile.7d2LEwDKoE8J
    1484653906720   geckodriver::marionette INFO    Starting browser C:\Program Files\Mozilla Firefox\firefox.exe
    1484653906731   geckodriver::marionette INFO    Connecting to Marionette on localhost:58602
    1484653908388   addons.manager  DEBUG   Application has been upgraded
    1484653908843   addons.manager  DEBUG   Loaded provider scope for resource://gre/modules/addons/XPIProvider.jsm: ["XPIProvider"]
    1484653908846   addons.manager  DEBUG   Loaded provider scope for resource://gre/modules/LightweightThemeManager.jsm: ["LightweightThemeManager"]
    1484653908852   addons.manager  DEBUG   Loaded provider scope for resource://gre/modules/addons/GMPProvider.jsm
    1484653908855   addons.manager  DEBUG   Loaded provider scope for resource://gre/modules/addons/PluginProvider.jsm
    1484653908857   addons.manager  DEBUG   Starting provider: XPIProvider
    1484653908857   addons.xpi  DEBUG   startup
    1484653908858   addons.xpi  INFO    SystemAddonInstallLocation directory

如何关闭此日志记录?

尝试了以下代码,但没有成功。似乎是 bug 中的 selenium 3.0

    LoggingPreferences pref = new LoggingPreferences();
    pref.enable(LogType.BROWSER, Level.OFF);
    pref.enable(LogType.CLIENT, Level.OFF);
    pref.enable(LogType.DRIVER, Level.OFF);
    pref.enable(LogType.PERFORMANCE, Level.OFF);
    pref.enable(LogType.PROFILER, Level.OFF);
    pref.enable(LogType.SERVER, Level.OFF);


    DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
    desiredCapabilities.setCapability(CapabilityType.LOGGING_PREFS, pref);

    WebDriver driver = new FirefoxDriver(desiredCapabilities);

    driver.get("https://www.google.com/");
    driver.findElement(By.id("lst-ib")).sendKeys("something");
    Thread.sleep(2000);
    driver.quit();
  GeckoDriverService gecko = new GeckoDriverService(new File("c:/selenium/geckodriver.exe"), 4444, ImmutableList.of("--log=fatal"), ImmutableMap.of());
  gecko.sendOutputTo(new FileOutputStream("gecko_log.txt"));
  gecko.start();

  FirefoxOptions opts = new FirefoxOptions().setLogLevel(Level.OFF);
  DesiredCapabilities capabilities = opts.addTo(DesiredCapabilities.firefox());
  capabilities.setCapability("marionette", true);
  FirefoxDriver driver = new FirefoxDriver(gecko, capabilities);

提出了一个对某些人有效的选项 ,并使用批处理文件将命令行参数传递给可执行文件。不幸的是,这通常会使额外的进程处于打开状态(geckodriver.exe、cmd.exe),并且目前还没有提出下一个问题的解决方案...

您可以通过系统 属性 将日志发送到 /dev/null 来禁用日志,如下所示:

System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");

return new FirefoxDriver();

Windows 和 Linux 上的工作解决方案。

# python 3

# windows
PATH_TO_DEV_NULL = 'nul'
FIREFOX_DRIVER_PATH = 'D:\path\to\geckodriver.exe'

# linux
PATH_TO_DEV_NULL = '/dev/null'
FIREFOX_DRIVER_PATH = '/path/to/geckodriver'

# start browser
driver = webdriver.Firefox(executable_path=FIREFOX_DRIVER_PATH,
                           service_log_path=PATH_TO_DEV_NULL)

这可能有点老套,但可以快速完成工作。鉴于您知道文件的确切位置并且 运行 您在 Linux 上的代码,您可以直接 cd 到该目录并

rm geckodriver.log
ln -s /dev/null geckodriver.log

service_log_path 似乎是 deprecated 在 python:

driver.py:77: DeprecationWarning: service_log_path has been deprecated, please pass in a Service object

这对我有用:

from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium import webdriver

service = FirefoxService(driver_path, log_path='nul') # Disable logs in windows, for linux probably /dev/null

driver = webdriver.Firefox(
    service=service
)