ARTICLE AD BOX
I have the following piece of code that I use to create webdrivers for my spring boot application.
public RemoteWebDriver createDriver() { ChromeOptions options = new ChromeOptions(); URL containerLocator; try { URI containerIdentifier = new URI(this.driverSocket); containerLocator = containerIdentifier.toURL(); } catch (URISyntaxException | MalformedURLException e) { this.logService.createInfoLog(this.messageService.createChapterURLExceptionMessage(this.driverSocket)); return null; } this.logService.createInfoLog(this.messageService.getLoggingInfoChapterCreatedDriver()); return new RemoteWebDriver(containerLocator, options); }This code is used to create drivers whenever I call my spring boot service to do some selenium parsing, for example-
@Async public CompletableFuture<FutureData> startParsingTask(String sessionId) { RemoteWebDriver driver = this.createDriver(); if (driver.getSessionId() != null) { this.logService.createInfoLog(this.messageService.getLoggingInfoActiveSessionFound()); driver.quit(); driver = this.createDriver(); } else { this.logService.createInfoLog(this.messageService.getLoggingInfoNoActiveSessionFound()); } // Try to parse and quit the driver after the attempt try { this.doParsingTask(driver, sessionId); this.logService.createInfoLog(this.messageService.getLoggingInfoParseSucceeded()); driver.quit(); this.logService.createInfoLog(this.messageService.getLoggingInfoQuitDriver()); } ... }I am not concerned with the second block of code. My issue is that due to the asynchronous nature of my selenium sessions, there are cases where the task will throw an exception and error out. This would leave the selenium container that the driver connects to stuck in the middle of a session and not properly quitted. If the spring boot backend then tries to run a task that requires the driver, it will just freeze at this line from the first block of code-
return new RemoteWebDriver(containerLocator, options);I have tested my program and determined that this is where my asynchronous tasks get stuck, but I do not know how to fix it using the first block of code. I have checked similar posts on Stack Overflow but they all give solutions that require the web driver object to be instantiated first, which I can't use because the program gets stuck at the instantiation.
Is there a way that I can modify the first block of code to create a web driver session without getting stuck due to the hung session in the selenium container?
For further context, here is the docker command I use to create the container-
docker run -d -p 4444:4444 -p 7900:7900 --shm-size="2g" selenium/standalone-chrome:136.0-20251101