I am getting the following error after I run selenium for a while; note I do call webdriver.quit(). Is there a way to simply tell selenium to close all open files (or solve this another way)? I recently started to see this; note that I set System.setProperty("webdriver.http.factory", "jdk-http-client");
to try to use the new (non-dprecated) jdk http client.
My exception and code:
java.io.UncheckedIOException: java.io.IOException: Too many open files
2023-03-23T00:43:26.044Z at java.net.http/jdk.internal.net.http.HttpClientImpl.<init>(HttpClientImpl.java:328)
2023-03-23T00:43:26.044Z at java.net.http/jdk.internal.net.http.HttpClientImpl.create(HttpClientImpl.java:270)
2023-03-23T00:43:26.044Z at java.net.http/jdk.internal.net.http.HttpClientBuilderImpl.build(HttpClientBuilderImpl.java:135)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.http.jdk.JdkHttpClient.<init>(JdkHttpClient.java:139)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.http.jdk.JdkHttpClient$Factory.createClient(JdkHttpClient.java:322)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:107)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:94)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.HttpCommandExecutor.<init>(HttpCommandExecutor.java:85)
2023-03-23T00:43:26.044Z at org.openqa.selenium.remote.service.DriverCommandExecutor.<init>(DriverCommandExecutor.java:80
My code:
My primary method calls
try {
localWebDriver = getWebDriver(useFastStrategy);
localWebDriver.get("about:blank");
localWebDriver.get(url);
localWebDriver.quit();
} catch (EXception e) {
quitSafely(localWebDriver);
}
...
Other support methods:
private void quitSafely(WebDriver localWebDriver) {
try {
if (localWebDriver == null) {
return;
}
localWebDriver.quit();
} catch (Exception e) {
// nothing to do
LOGGER.info("Got error in quit() call of quitSafely", e);
}
}
private WebDriver getWebDriver(boolean useFastStrategy) {
ChromeDriverService service = new ChromeDriverService.Builder()
.withVerbose(false)
.withSilent(true)
.build();
try {
java.util.logging.Logger.getLogger("org.openqa.selenium").setLevel(Level.WARNING);
// service.sendOutputTo(new FileOutputStream("/dev/null"));
service.sendOutputTo(OutputStream.nullOutputStream());
} catch (Exception e) {
LOGGER.error("Unable to suppress output");
}
return new ChromeDriver(service, getChromeOptions(useFastStrategy));
}
private ChromeOptions getChromeOptions(boolean useFastStrategy) {
ChromeOptions chromeOptions = new ChromeOptions();
// without this it will fail, see:
// https://stackoverflow.com/questions/75718422/org-openqa-selenium-remote-http-connectionfailedexception-unable-to-establish-w
// and https://www.selenium.dev/blog/2022/using-java11-httpclient/
// You either have to enable this or to set 'System.setProperty("webdriver.http.factory", "jdk-http-client");'
chromeOptions.addArguments("--remote-allow-origins=*");
if (useFastStrategy) {
chromeOptions.setPageLoadStrategy(PageLoadStrategy.EAGER); // was default (Normal) before.
}
// User agent is required because some websites will reject your request if it does not have a user agent
chromeOptions.addArguments(String.format("user-agent=%s", USER_AGENT));
chromeOptions.addArguments("--log-level=OFF");
chromeOptions.addArguments("--headless=new");
// chromeOptions.setHeadless(true); // deprecated
List<String> arguments = new LinkedList<>();
arguments.add("--disable-extensions");
arguments.add("disable-infobars"); // try enabling this to try and save some cpu
arguments.add("--headless");
arguments.add("--disable-gpu");
arguments.add("--no-sandbox");
arguments.add("--incognito");
arguments.add("--disable-application-cache");
arguments.add("--disable-dev-shm-usage");
chromeOptions.addArguments(arguments);
return chromeOptions;
}