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?
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?
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()
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
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());
}
}
}