I am running a Selenium WebDriver (ChromeDriver) in Java to do some basic automation, requiring user input. The user is able to, at any time, click the close (red X) button of the window. This doesn't call driver.quit()
to properly quit the driver, leaving it running in the background and throwing a WebDriverException
, which I don't want to happen. I am aware that there are no event listeners for browser window actions. I tried doing this:
WebDriver driver = null;
try {
driver = new ChromeDriver();
//do more stuff here
WebDriverWait wait = new WebDriverWait(driver, 120);
wait.until(
//waiting conditions
);
} catch (WebDriverException e) { //thrown after can't reach browser (browser closed)
//handle exception
} finally {
if (driver != null)
driver.quit();
}
//continue with non-WebDriver parts of program
However, I need the program to move on quickly, and it often takes several seconds after the window is closed for the exception to be thrown. Also, this method just feels like bad programming anyway.
Is there a better way to quit ChromeDriver after it is manually closed?