How do I disable Firefox logging in Selenium using Geckodriver?

不问归期 提交于 2019-12-05 13:49:07

问题


I am using:

  • firefox version 50.1.0
  • geckodriver version 0.11.1
  • selenium-java 3.0.1

I have tried

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("webdriver.log.browser.ignore", true);
profile.setPreference("webdriver.log.driver.ignore", true);
profile.setPreference("webdriver.log.profiler.ignore", true);
FirefoxDriver driver = new FirefoxDriver();

and

LoggingPreferences preferences = new LoggingPreferences();
preferences.enable(LogType.BROWSER, Level.OFF);
preferences.enable(LogType.CLIENT, Level.OFF);
preferences.enable(LogType.DRIVER, Level.OFF);
preferences.enable(LogType.PERFORMANCE, Level.OFF);
preferences.enable(LogType.SERVER, Level.OFF);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(CapabilityType.LOGGING_PREFS, preferences);
FirefoxDriver driver = new FirefoxDriver(capabilities);

neither of these methods does anything to stop logging. Here is console output if that helps somehow:

  • first method: http://pastebin.com/23nate2G
  • second method: http://pastebin.com/NwmWEeXT

For those wondering, i have log4j 1.2.17 in my pom.xml but have no log4j.properties or log4j.xml and I do not use it at all.


To clarify: when I say logging I mean the console output in IntelliJ IDEA. I am using Java.


回答1:


To not see the logs in the console you can use the following:

System.setProperty("webdriver.gecko.driver","src/main/resources/drivers/geckodriver.exe");
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");

return new FirefoxDriver();



回答2:


You can define the desired logging level over command line in geckodriver.exe.

geckodriver.exe -help    
USAGE:
    geckodriver.exe [FLAGS] [OPTIONS]   
...
OPTIONS:
        --log <LEVEL>
            Set Gecko log level [values: fatal, error, warn, info, config,
            debug, trace]

If you use geckodriver from selenium, you have two option:

  • Start geckodriver.exe separately with custom arguments, and use it from selenium over RemoteWebDriver
  • Create a custom wrapper, to add extra parameters to geckodriver.exe

Example geckodriver wrapper bat file (for windows):

@ECHO OFF
ECHO Starting geckodriver: %0 %*
.\GeckoDriver\geckodriver.exe --log fatal %* > NUL 2>&1

In java you can define the geckodriver executable path, over webdriver.gecko.driver system property:

System.setProperty("webdriver.gecko.driver", "c:/selenium/geckodriver/gdrvwrapper.bat");



回答3:


Just do this

System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
driver = new FirefoxDriver();



回答4:


This is the linux version for @voji answer above. Note as I said above in the comment. I don't believe the --log fatal does anything, at least not on linux. However the redirect to NULL works well enough for me

"webdriver.gecko.driver": "/path-to-driver/geckodriver.sh

filename: geckodriver.sh (executable)

#! /bin/bash
echo " ARGS: " $@
geckodriver --log fatal "$@" > /dev/null 2>&1



回答5:


For selenium 3.14.0 , this is working

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



回答6:


import org.openqa.selenium.firefox.FirefoxDriver
System.setProperty(
  "webdriver.gecko.driver",
  "C:\Users\geckodriver-v0.22.0-win64\geckodriver.exe"
)
System.setProperty(
  FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,
  "true"
)
System.setProperty(
  FirefoxDriver.SystemProperty.BROWSER_LOGFILE,
  "d:\eclipse\scala_log.txt"
)



回答7:


  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);
  driver = new FirefoxDriver(gecko, capabilities);


来源:https://stackoverflow.com/questions/41387794/how-do-i-disable-firefox-logging-in-selenium-using-geckodriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!