Free Selenium chromedriver.exe from memory

I installed python code to run Selenium chromedriver.exe . At the end of the execution, I have browser.close() to close the instance. ( browser = webdriver.Chrome() ) I believe that it should release chromedriver.exe from memory (I'm on Windows 7). However, after each start, one instance of chromedriver.exe remains in memory. I hope there is a way to write something in python to kill the chromedriver.exe process. Obviously browser.close() does not do this work. Thank.

+77
selenium selenium-webdriver selenium-chromedriver
Jan 23 '14 at 22:40
source share
21 answers

for the Selenium API, you really should call browser.quit() as this method closes all windows and kills the process. You should use browser.quit() .

However: in my workplace, we noticed a huge problem when trying to run chrome tests on the Java platform, where chromedriver.exe actually exists even after using browser.quit() . To counter this, we created a batch file similar to the one below, which simply forces processes to close.

kill_chromedriver.bat

 @echo off rem just kills stray local chromedriver.exe instances. rem useful if you are trying to clean your project, and your ide is complaining. taskkill /im chromedriver.exe /f 

Since chromedriver.exe is not a huge program and does not consume a lot of memory, you do not need to run it every time, but only when a problem occurs. For example, when starting Project-> Clean in Eclipse.

+59
Jan 23 '14 at 22:48
source share

browser.close() closes only the current chrome window.

browser.quit() should close all open windows and then exit webdriver.

+25
Jan 23 '14 at 22:47
source share
Theoretically, calling browser.Quit closes all browser tabs and kills the process.

However, in my case, I could not do this - since I was running several tests in parallel, I did not want one test to close windows with another. Therefore, when my tests are over, there are still many "chromedriver.exe" processes left.

To overcome this, I wrote a simple cleanup code (C #):

 Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver"); foreach(var chromeDriverProcess in chromeDriverProcesses) { chromeDriverProcess.Kill(); } 
+13
Feb 29 '16 at 5:05
source share

I had success using driver.close() before driver.quit() . I used to use only driver.quit() .

+9
May 17 '17 at 5:13
source share
 //Calling close and then quit will kill the driver running process. driver.close(); driver.quit(); 
+6
Dec 05 '16 at 0:56
source share

This answer is how to properly utilize a driver in C #

If you want to use the "correct" mechanism that you should use for " ChromeDriver " after starting ChromeDriver you should use IWebDriver.Dispose();

Performs user-defined tasks related to the release, release, or dump of unmanaged resources. (Inherited from IDisposable.)

I usually implement IDisposable on a class that deals with IWebDriver

 public class WebDriverController : IDisposable { public IWebDriver Driver; public void Dispose() { this.Driver.Dispose(); } } 

and use it like:

 using (var controller = new WebDriverController()) { //code goes here } 

Hope this saves you some time

+4
May 23 '17 at 5:02 a.m.
source share

This is weird, but it works for me. I had a similar problem, after some digging, I found that the browser still has a user interface action (loading a URL or so) when I clicked WebDriver.Quit() .

The solution for me (quite nasty) was to add Sleep() 3 seconds before calling Quit ().

+3
Jan 30 '15 at 9:35
source share

C # code

using System.Diagnostics;

using System.Management;

  public void KillProcessAndChildren(string p_name) { ManagementObjectSearcher searcher = new ManagementObjectSearcher ("Select * From Win32_Process Where Name = '"+ p_name +"'"); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { try { KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); } catch (ArgumentException) { break; } } } 

and this function

  public void KillProcessAndChildren(int pid) { ManagementObjectSearcher searcher = new ManagementObjectSearcher ("Select * From Win32_Process Where ParentProcessID=" + pid); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { try { KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); } catch { break; } } try { Process proc = Process.GetProcessById(pid); proc.Kill(); } catch (ArgumentException) { // Process already exited. } } 

Call

  try { KillProcessAndChildren("chromedriver.exe"); } catch { } 
+3
Feb 26 '17 at 13:17
source share

I had the same issue when running in Python, and I had to manually run the killall command to kill all the processes. However, when I implemented the driver using the Python protocol, the context management protocol , all processes disappeared. It seems that the Python interpreter is doing a really good job of cleaning things up.

Here is the implementation:

 class Browser: def __enter__(self): self.options = webdriver.ChromeOptions() self.options.add_argument('headless') self.driver = webdriver.Chrome(chrome_options=self.options) return self def __exit__(self, exc_type, exc_val, exc_tb): self.driver.close() self.driver.quit() 

And use:

 with Browser() as browser: browser.navigate_to_page() 
+2
Dec 03 '17 at 19:15
source share

I used below in nightwatch.js in the afterEach traps.

 afterEach: function(browser, done) { // performing an async operation setTimeout(function() { // finished async duties done(); browser.closeWindow(); browser.end(); }, 200); } 

.closeWindow () just closes the window. (But will not work for multiple open windows). Whereas .end () terminates all remaining Chrome processes.

+1
Jul 04 '18 at 14:37
source share

Python Code:

 try: # do my automated tasks except: pass finally: driver.close() driver.quit() 
+1
Oct 18 '18 at 21:31
source share

So you can use the following:

 driver.close() 

Close the browser (emulates clicking the close button)

 driver.quit() 

Exit the browser (emulates the choice of the exit option)

 driver.dispose() 

Exit the browser (try closing each tab and exit)

However, if you still have problems with instance freezes (like me), you can also kill the instance. To do this, you need the PID of the Chrome instance.

 import os import signal driver = webdriver.Chrome() driver.get(('http://stackoverflow.com')) def get_pid(passdriver): chromepid = int(driver.service.process.pid) return (chromepid) def kill_chrome(thepid) try: os.kill(pid, signal.SIGTERM) return 1 except: return 0 print ("Loaded thing, now I'mah kill it!") try: driver.close() driver.quit() driver.dispose() except: pass kill_chrome(chromepid) 

If thereโ€™s a copy of Chrome left after that, I'll eat my hat. :(

+1
Oct 22 '18 at 18:24
source share

I know this is a somewhat old question, but I thought I was sharing what worked for me. I am having problems with Eclipse - this will not kill processes, and therefore I had a bunch of phantom processes freezing after testing the code with the Eclipse runner.

My solution was to run Eclipse as an administrator. This fixed it for me. It seems that Windows did not allow Eclipse to shut down the process that it spawned.

0
Nov 21 '16 at 17:01
source share

I have this problem. I suspect this is due to the version of Serenity BDD and Selenium. The chrome process is never released until the entire test suite is complete. There are only 97 tests, but 97 processes are eating up the memory of a server that does not have many resources, which can affect performance.

For addressing, I did 2 things (this applies to windows).

  • before each test (annotated with @Before), it receives the process identifier (PID) of the chrome process using:

     List<Integer> pids = new ArrayList<Integer>(); String out; Process p = Runtime.getRuntime().exec("tasklist /FI \"IMAGENAME eq chromedriver.exe*\""); BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((out = input.readLine()) != null) { String[] items = StringUtils.split(out, " "); if (items.length > 1 && StringUtils.isNumeric(items[1])) { pids.add(NumberUtils.toInt(items[1])); } } 
  • after each test (annotated with @After) kills the PID with:

     Runtime.getRuntime().exec("taskkill /F /PID " + pid); 
0
May 26 '17 at 6:47 a.m.
source share

I came here initially, thinking for sure that this would be answered / allowed, but after reading all the answers I was a little surprised, no one tried to name all three methods together:

 try { blah } catch { blah } finally { driver.Close(); // Close the chrome window driver.Quit(); // Close the console app that was used to kick off the chrome window driver.Dispose(); // Close the chromedriver.exe } 

I was only here to look for answers and was not going to provide it. So the above solution is based only on my experience. I used the chrome driver in a C # console application, and I was able to clear lingering processes only after calling all three methods together.

0
Jul 27 '17 at 12:10
source share

For Ubuntu / Linux users: - This is either pkill or killall . It is generally recommended to use pkill , since on some systems killall actually kills all processes.

0
Mar 08 '18 at 6:40
source share

Kill multiple processes from the command line The first thing you need to do is open a command prompt and then use the taskkill command with the following syntax:

 taskkill /F /IM <processname.exe> /T 

These parameters will forcefully kill any process matching the name of the executable file that you specify. For example, to kill all iexplore.exe processes, use:

 taskkill /F /IM iexplore.exe 

enter image description here

0
Aug 25 '18 at 6:48
source share

I am using a protractor with DirectConnect. Disabling the --no-sandbox option solved the problem for me.

 // Capabilities to be passed to the webdriver instance. capabilities: { 'directConnect': true, 'browserName': 'chrome', chromeOptions: { args: [ //"--headless", //"--hide-scrollbars", "--disable-software-rasterizer", '--disable-dev-shm-usage', //"--no-sandbox", "incognito", "--disable-gpu", "--window-size=1920x1080"] } }, 
0
Oct 11 '18 at 20:31
source share
  • Make sure you get the Driver instance as Singleton
  • then apply at the end
  • driver.close ()
  • driver.quit ()

Note. Now, if we see the task manager, you will not find a single driver or chrome process that still hangs.

0
Feb 05 '19 at 21:32
source share

I looked at all the answers and checked them all. I pretty much compiled them all into one as Security Closure. This is in C #

Please note that you can change the parameter from the IModule application to the actual driver parameter.

  public class WebDriverCleaner { public static void CloseWebDriver(IModule app) { try { if (app?.GetDriver() != null) { app.GetDriver().Close(); Thread.Sleep(3000); // Gives time for everything to close before quiting app.GetDriver().Quit(); app.GetDriver().Dispose(); KillProcessAndChildren("chromedriver.exe"); // One more to make sure we get rid of them chromedrivers. } } catch (Exception e) { Console.WriteLine(e); throw; } } public static void KillProcessAndChildren(string p_name) { ManagementObjectSearcher searcher = new ManagementObjectSearcher ("Select * From Win32_Process Where Name = '" + p_name + "'"); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { try { KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); } catch (ArgumentException) { break; } } } public static void KillProcessAndChildren(int pid) { ManagementObjectSearcher searcher = new ManagementObjectSearcher("Select * From Win32_Process Where ParentProcessID=" + pid); ManagementObjectCollection moc = searcher.Get(); foreach (ManagementObject mo in moc) { try { KillProcessAndChildren(Convert.ToInt32(mo["ProcessID"])); } catch { break; } } try { Process proc = Process.GetProcessById(pid); proc.Kill(); } catch (ArgumentException) { // Process already exited. } } } 
0
Feb 25 '19 at 21:50
source share

On the bottom line ... you need to somehow kill the process, because the designers did not enter it. All other methods can lead to the start of the process, and this is enough to guarantee killing. I am open to options.

-one
Jan 16 '15 at 17:27
source share



All Articles