Selenium initiated ChromeDriver keeps running in background
Asked Answered
E

5

5

I am not able to completely delete a project, because the chromedriver instance is running in the background, even when there is no code is being executed. Please see the below image. Task Manager Process View

The error I get while the project deletion is:Eclipse error while project deletion

Take the below code for instance:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class Test {

    public static void main(String[] args) throws Exception {

        String url = "https://www.google.com";
        System.setProperty("webdriver.chrome.driver", "src/driver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get(url);
        System.out.println(driver.getTitle());

    }
}

Try running this code, say 5 times, and there will be 5 background running instances. Tried restarting Eclipse, with no luck.

I understand this is happening because I am not writing this line:

driver.close();

But normally, when the main-thread dies, all supporting running instances should die with it.

Is this a known issue, or I am scripting something wrong.

Thanks in advance.

Elroyels answered 17/4, 2017 at 5:17 Comment(0)
K
7

You need to take care of a couple of things as follows:

  1. Try running this code, say 5 times, and there will be 5 background running instances: This is because each time you have opened a new driver instance which you have never released.

  2. Tried restarting Eclipse, with no luck: Its worth mentioning that Eclipse is an IDE (Integrated Development Environment) which helps us to construct programming steps through a programming language. So through Eclipse we write programs/steps for Selenium to open/close a browser session and quit a driver session. But Eclipse have no control over the execution done by Selenium in-terms of open/close a browser session and quit a driver session. Hence restarting Eclipse would make no difference.

  3. driver.close(): This command will help you only to Close the browser session/window on which currently the driver have the focus.

  4. when the main-thread dies, all supporting running instances should die with it: You are right. It is the WebDriver instance (i.e. driver - as per your code) which opens all the browser sessions every-time. So when your program ends you should always call driver.quit() which Closes all browser windows, quits the driver and safely ends the session.


However, at any point of time you can forcefully kill all the instances of WebDriver variants e.g. , , through specific methods as follows:

  • Java Solution(Windows):

    import java.io.IOException;
    
    public class Kill_ChromeDriver_GeckoDriver_IEDriverserver 
    {
        public static void main(String[] args) throws Exception 
        {
            Runtime.getRuntime().exec("taskkill /F /IM geckodriver.exe /T");
            Runtime.getRuntime().exec("taskkill /F /IM chromedriver.exe /T");
            Runtime.getRuntime().exec("taskkill /F /IM IEDriverServer.exe /T");
        }
    }
    
  • Python Solution(Windows):

    import os
    os.system("taskkill /f /im geckodriver.exe /T")
    os.system("taskkill /f /im chromedriver.exe /T")
    os.system("taskkill /f /im IEDriverServer.exe /T")
    
  • Python Solution(Cross Platform):

    import os
    import psutil
    
    PROCNAME = "geckodriver" # or chromedriver or IEDriverServer
    for proc in psutil.process_iter():
        # check whether the process name matches
        if proc.name() == PROCNAME:
            proc.kill()
    
Keikokeil answered 17/4, 2017 at 13:42 Comment(1)
Yes. Thank you for the detailed answer. Appreciate your in-depth analysisElroyels
H
3

As chromedriver is an executable file, so it is run as a separate thread and it isn't getting closed because your driver.close() method is not getting called so you have no other choice than to close it manually. You can either choose to close chromedriver.exe process manually from Task Manager but I'd recommend using following command in Command Prompt

 taskkill /f /im chromedriver.exe

This particular command can be run in order to delete any processes running on your windows device.

Heterochromatic answered 17/4, 2017 at 5:44 Comment(0)
B
2

I know this is off topic, but this helped me to solve the problem chromedriver is still running in the background. I was using driver.close which is not killing the session. See this answer:

You should use driver.quit whenever you want to end the program. It will close all opened browser windows and terminates the WebDriver session. If you do not use driver.quit at the end of the program, the WebDriver session will not close properly and files would not be cleared from memory. This may result in memory leak errors.

Bosomed answered 3/2, 2023 at 14:53 Comment(0)
B
1

When executing the chrome-driver, you must close it ,as you mentioned, in order to close the chrome-driver. Basically in order to run tests you should use relevant running framework as JUnit(as for java) and then you won't be confused.

Butanol answered 17/4, 2017 at 5:27 Comment(4)
Thanks Yaniv... I understand this, but what I am not able to understand is the running instances of chromedriver should get closed, atleast when I close Eclipse, shouldn't it? Also, why there would be multiple instances?Elroyels
Each time you call the chromedriver.exe a different instance is being opened. It is a standalone process which you must close and it has nothing to Eclipse. As you called it to be opened , you should call its close method.Butanol
I just tried using driver.close(); and same thing happens, while using driver.quit(); works as I expect it to be. There should not be any difference while working on a single window, both should work the same, I believe.Elroyels
Sure. Close just closes the chrome's window(s) and quit is actually for quitting the driver.Butanol
T
1

Create an bat file(killchorme.bat) using the below cmd

taskkill /f /im chromedriver.exe

then call the bat file before initiating your chrome driver using Runtime.getruntime().exec("killchrome.bat")

Tallboy answered 17/4, 2017 at 13:59 Comment(1)
this is one way of doing or simply use driver.quit(); at the end of the code, as suggested by othersElroyels

© 2022 - 2024 — McMap. All rights reserved.