Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

As per your question commenting out driver.quit() just Not to close firefox window after each run, because I just want to analyse what I have won’t be a part of best practices.

For any detailed analysis we can create log entries and take snapshots.

While automating through Selenium as per the best practices you should invoke the quit() method within the tearDown() {}. Invoking quit() DELETEs the current browsing session through sending “quit” command with {“flags”:[“eForceQuit”]} and finally sends the GET request on /shutdown EndPoint. Here is an example below :

1503397488598   webdriver::server   DEBUG   -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74 
1503397488607   geckodriver::marionette TRACE   -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821   webdriver::server   DEBUG   -> GET /shutdown

So on invoking quit() method the Web Browser session and the WebDriver instance gets killed completely. Hence you don’t have to incorporate any additional steps which will be an overhead.


Solution

Still if you want to execute kill the dangling WebDriver instances e.g. GeckoDriver.exe instances you can use either of the following code block to kill any of the dangling WebDriver instances :

  • 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()
    

Leave a Comment