8

I am trying to use selenium in a mini web crawler to get the page source. My output log is invaded by selenium logs, is there a way to totally disable the logging or just redirect it in someway to /dev/null?

The logging messages are these:

Starting ChromeDriver 2.43.600233 
(523efee95e3d68b8719b3a1c83051aa63aa6b10d) on port 1628
Only local connections are allowed.
ott 24, 2018 7:52:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFORMAZIONI: Detected dialect: OSS

I am calling the driver in the following way:

WebDriver driver = null;
            try {
            System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.setBinary("/usr/bin/chromium");
            chromeOptions.addArguments("--headless");
            chromeOptions.addArguments("--silent");
            chromeOptions.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
            driver = new ChromeDriver(chromeOptions);
            /*FirefoxBinary firefoxBinary = new FirefoxBinary();
            firefoxBinary.addCommandLineOptions("--headless");
            System.setProperty("webdriver.gecko.driver", "/usr/local/bin/geckodriver");
            System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE, "true");
            System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE, "/dev/null");


            FirefoxOptions firefoxOptions = new FirefoxOptions();
            firefoxOptions.setBinary(firefoxBinary);
            FirefoxDriver driver = new FirefoxDriver(firefoxOptions);*/
            if(driver!=null) {
            driver.get(link);
Drunk Cat
  • 483
  • 1
  • 4
  • 9
  • may be you should look at this [thread](https://stackoverflow.com/questions/18702533/how-to-execute-selenium-chrome-webdriver-in-silent-mode) – Dev Oct 24 '18 at 19:28
  • Ok it is possible to get rid of: `Starting ChromeDriver 2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d) on port 1628 Only local connections are allowed.` Using: `System.setProperty("webdriver.chrome.silentOutput", "true");` But `ott 24, 2018 7:52:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMAZIONI: Detected dialect: OSS` is still there. – Drunk Cat Oct 24 '18 at 20:06

2 Answers2

25

Ok i have managed to finally get rid of that useless loggings. Here is what i did.
Use:
System.setProperty("webdriver.chrome.silentOutput", "true");

To get rid of chromedriver logs:

Starting ChromeDriver 2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d) on port 1628 Only local connections are allowed.


And use: java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
To get rid of selenium logs:

ott 24, 2018 7:52:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMAZIONI: Detected dialect: OSS

Drunk Cat
  • 483
  • 1
  • 4
  • 9
  • Why are these useless? You start having issues and probably the first thing you are going to do is to turn those back on... Just ignore them. – JeffC Oct 25 '18 at 19:14
  • 3
    @JeffC any logging that is not requested or error/warning is going to spam the reader with so much garbage they won't notice an issue when it does occur. And The UNIX philosophy agrees; good command line tools have no output on success. – user1133275 May 30 '19 at 04:35
  • You shouldn't be looking at console output for test run results. You should be using a test runner framework like TestNG or JUnit to run your tests and get results. Better yet use a plugin for Jenkins (or whatever) that pulls those TestNG, etc. results into a report to make it even cleaner, have a history, etc. – JeffC May 30 '19 at 16:05
  • 5
    lots of "ChromeDriver was started successfully." messages from v83 ... see my response below – MetalRules Jun 26 '20 at 04:57
  • There seems to be a solution for the `ChromeDriver was started successfully` thing, but I'm not really liking it. https://groups.google.com/g/chromedriver-users/c/QipZrjyoEgg – gaoagong Dec 06 '21 at 18:46
8

Drunk Cat's answer is right and very useful to get rid of 100's of pointless info messages in a log. Maybe use java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.SEVERE);
to catch errors (Level.SEVERE instead of Level.OFF)

Chromedriver v83 (2020 Update)
Note that an alternate to setting the property:

System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true"); 

is something like this:

DriverService.Builder serviceBuilder = new ChromeDriverService.Builder().withSilent(true);
ChromeOptions options = new ChromeOptions();
// ... addArguments to options ....
ChromeDriverService chromeDriverService = (ChromeDriverService)serviceBuilder.build(); 
ChromeDriver driver = new ChromeDriver(chromeDriverService, options);

However, for whatever reason neither .withSilent(true) or setting the property work on Chromedriver v83 (confirmed on Linux and Windows). I needed to add a line of code to re-direct output:

:
ChromeDriverService chromeDriverService = (ChromeDriverService)serviceBuilder.build(); 
chromeDriverService.sendOutputTo(new FileOutputStream("/dev/null"));
ChromeDriver driver = new ChromeDriver(chromeDriverService, options);

You could just substitute "/dev/nul" with a real file if you wanted (for debugging, etc). Or a platform independent way of dealing with null output that works with Java 8+ :

chromeDriverService.sendOutputTo(new OutputStream(){@Override public void write(int b){}});

My guess is that this may be a bug in the v83 release, but at least it got me to find another way of re-directing or shutting off chromedriver logging.

MetalRules
  • 189
  • 1
  • 5