Dru*_*Cat 3 java logging selenium selenium-chromedriver
我正在尝试在小型Web搜寻器中使用Selenium来获取页面源。我的输出日志受到硒日志的入侵,有没有一种方法可以完全禁用日志记录,或者只是以某种方式将其重定向到/ dev / null?
日志消息是:
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
Run Code Online (Sandbox Code Playgroud)
我通过以下方式调用驱动程序:
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);
Run Code Online (Sandbox Code Playgroud)
Dru*_*Cat 11
好吧,我终于设法摆脱了无用的日志。这是我所做的。
使用:
System.setProperty("webdriver.chrome.silentOutput", "true");
摆脱chromedriver日志:
在端口1628上启动ChromeDriver 2.43.600233(523efee95e3d68b8719b3a1c83051aa63aa6b10d)仅允许本地连接。
并使用:
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF);
摆脱硒记录:
ott 24,2018 7:52:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFORMAZIONI:检测到方言:OSS
Drunk Cat 的回答是正确的,对于摆脱日志中数百条毫无意义的信息消息非常有用。可能用于java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.SEVERE);
捕获错误(Level.SEVERE 而不是 Level.OFF)
Chromedriver v83(2020 更新)
请注意,设置属性的替代方法:
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
Run Code Online (Sandbox Code Playgroud)
是这样的:
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);
Run Code Online (Sandbox Code Playgroud)
但是,无论出于何种原因,.withSilent(true)该属性都无法在 Chromedriver v83(在 Linux 和 Windows 上确认)上运行。我需要添加一行代码来重定向输出:
:
ChromeDriverService chromeDriverService = (ChromeDriverService)serviceBuilder.build();
chromeDriverService.sendOutputTo(new FileOutputStream("/dev/null"));
ChromeDriver driver = new ChromeDriver(chromeDriverService, options);
Run Code Online (Sandbox Code Playgroud)
如果需要(用于调试等),您可以将“/dev/nul”替换为真实文件。或者一种处理与 Java 8+ 一起使用的空输出的平台独立方式:
chromeDriverService.sendOutputTo(new OutputStream(){@Override public void write(int b){}});
Run Code Online (Sandbox Code Playgroud)
我的猜测是这可能是 v83 版本中的一个错误,但至少它让我找到了另一种重定向或关闭 chromedriver 日志记录的方法。
| 归档时间: |
|
| 查看次数: |
2253 次 |
| 最近记录: |