Skip to content
Advertisement

How to kill the Chrome driver process once the browser is closed with Selenium

I made a stand-alone GUI for my Selenium project, and I noticed that after using it for a while, I had 10+ chrome drivers opened in task manager. I realize this is because driver.quit() is never called. I need to make it to where in the event that the chrome browser is closed, the driver quits. How do I do this?

edit: I can make the driver quit once the process is done, but sometimes I want to close the browser and quit the process in the middle of it running, meaning the code that quits the driver wouldn’t be reached. This is why I need to quit the driver in the event that the chrome window is closed.

edit: Ill give an example:

public static void runJoinGroup(String groupLink, String accounts) {
        Scanner sc = new Scanner(accounts);
        process(sc);
        launchBrowser();
        while (x < users.size()) {
            login(users.get(x), pass.get(x));
            x++;
            joinGroup(groupLink);
            logout();
        }
    }

this is the method that one of the buttons on my GUI executes when pressed. I could quit the driver after the method is finished running, like this:

public static void runJoinGroup(String groupLink, String accounts) {
        Scanner sc = new Scanner(accounts);
        process(sc);
        launchBrowser();
        while (x < users.size()) {
            login(users.get(x), pass.get(x));
            x++;
            joinGroup(groupLink);
            logout();
        }
        driver.quit();
    }

However, if I close the browser before it is finished logging into all the accounts (before x>=users.size()), driver.quit() will never be executed and I will still have the chrome driver running in the background. This is what I am trying to prevent.

edit: Here is a clear, reproducible example:

public static void example() {
        System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        for (int i = 0; i < 100; i++) {
            driver.get("google.com");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    driver.quit();

    }

This example code visits google.com and then sleeps for 5 seconds 100 times. If I decide to press the ‘X’ button before google.com has been visited 100 times, the line containing driver.quit() will not have been reached, and therefore the chrome driver will continue to run in the background. This is what I am trying to prevent. I want to make it to where if I decide to close out of the chrome browser (by pressing the ‘X’ button at the top right hand corner of the browser) BEFORE the process is finished, it will quit the driver.

Here’s an image of how the GUI works: https://imgur.com/a/Vw5oPSU (Zoom in to see the labels I made).

Advertisement

Answer

Surrounding all of my methods with

try{
    method()
} finally{
   Runtime.getRuntime().exec("taskkill /F /IM ChromeDriver.exe")
}

ended up solving the issue. After the method is either done being run or some exception caused it to stop running (like closing the browser), all instances of ChromeDriver.exe are killed.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement