Selenium chromedriver disable logging or redirect it java

前端 未结 2 525
臣服心动
臣服心动 2021-01-13 02:47

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 re

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 03:43

    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.

提交回复
热议问题