简体   繁体   中英

How do I disable Firefox logging in Selenium using Geckodriver?

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:

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.

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();

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");

Just do this

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

I am using headless Firefox driver in C#.
To disable geckoDriver logging console, below code it seems to be working for me:

FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory());
service.HideCommandPromptWindow = true;
FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--headless");
options.LogLevel = FirefoxDriverLogLevel.Fatal;
WebDriver = new FirefoxDriver(service, options);

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

对于 selenium 3.14.0 ,这是有效的

System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"/dev/null");
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"
)

This might be a bit hacky but it can quickly get the job done without any coding required. Given that you know the exact location of your file and you run your code on Linux you can just cd into that dir and

rm geckodriver.log
ln -s /dev/null geckodriver.log
profile.setPreference("webdriver.log.init", true);

有效,因为它使 webdriver 相信日志已经被初始化,所以它不会初始化它并且不会产生任何日志。

  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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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