1

I use Selenium to perform automation. However, if I repeat automation, I have a lot of chromedrivers.

I want to solve this problem. Can't you just run one chromedriver?

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Yeonwoo Jeong
  • 33
  • 1
  • 6

3 Answers3

0

As per the screenshot you have provided there seems to be presence of a couple of Zombie ChromeDriver process within your system.

Answering straight, you can't work with initiating just one ChromeDriver process while repeat automation as you can not reconnect to the previous browsing session. You can find a detailed discussion in How can I reconnect to the browser opened by webdriver with selenium?

You code trials would have given us more insights why the ChromeDriver processes are not getting cleaned up. As per best practices always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully as follows:

driver.quit() // Python
//or
driver.quit(); // Java
//
driver.Quit(); // DotNet

You can find a detailed discussion in PhantomJS web driver stays in memory

Incase the ChromeDriver processes are still not destroyed and removed you may require to kill the processes from tasklist. You can find a detailed discussion in Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

  • 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()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • The answer is factually incorrect - that processes are not zombie one (that's very hard to achieve in Windows, borderline impossible), but orphaned - functional processes which parent is no longer present. The next one is reconnecting to an existing driver - OP hasn't asked for it, and yet - it's not that it is not possible, on the contrary - it is quite easy to do so, and very useful for development purposes. – Todor Minakov Dec 29 '18 at 16:35
  • Finally, the `proc.name()` solution is hardly cross platform - the name in the sample is Linux one, Windows names have .exe in them. The `kill()` method sends sigterm, which the processes may handle the way it wants, being a zombie - if it was a zombie process in the first place, which it is not - with no action at all. – Todor Minakov Dec 29 '18 at 16:39
  • 1
    furthermore, psutil requires an additional dependency – Corey Goldberg Dec 30 '18 at 06:45
0

The reason you're left with the *driver.exe processes is most probably because you are not explicitly closing them at the end of your tests run - calling the quit() method on the driver object in your chosen language.

That step is usually done in the object's destructor - if you're using object-oriented approach; a finally block if it's exception-handling; or the the program's/script's exit lines. Most high-level frameworks (Cucumber, TestNG, Robotframework, the plethora of unit testing in the different languages) have some kind of "tear-down" blocks which is usually used for this purpose.


Why is this happening?

When you start your automation, the OS starts the process for it; when you instantiate a webdriver object, it spawns a process for the browser's driver - the "chromedriver.exe" in your case. The next step is to open the browser instance - the "chrome.exe".

When your run finishes, the process for it is closed. But, if you have not explicitly called the quit() method - the browser's driver persists, "stays alive"; and is now an orphaned process (not to be mistaken with a zombie one, which is a totally different thing) - fully functional, yet - with no program to command it.

In fact, at this stage - having a working driver process and a browser, you can reconnect to it, and use in future runs. Check out how and why here - https://stackoverflow.com/a/52003231/3446126

Todor Minakov
  • 19,097
  • 3
  • 55
  • 60
0

I use a cleanup routine at the end of my tests to get rid of zombie processes, temp files, etc. (Windows, Java)

import org.apache.commons.io.FileUtils;
import java.util.logging.Logger;
  
import java.io.File;
import java.io.IOException;
    
    
public class TestCleanup {

    protected static Logger logger = Logger.getLogger(TestCleanup.class.getName());


    public void removeTemporaryFiles() {
        try {
            FileUtils.cleanDirectory(new File(getPropertyValue("java.io.tmpdir")));
        } catch (IOException ioException) {
            logger.info("IOexception trying to clean the downloads directory: " + ioException.getMessage());
        }
    }

    public void closeWebdriver() {
        webDriver.close();
    }

    public void killOperatingSystemSessions() {
        try {
            Process process = Runtime.getRuntime().exec("taskkill /IM \"chromedriver.exe\" /F");
        } catch (IOException ioException) {
            logger.info("Exception when shutting down chromedriver process: " + ioException.getMessage());
        }
    }

}
ba-a-aton
  • 574
  • 1
  • 4
  • 14