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

前端 未结 1 749
孤城傲影
孤城傲影 2020-11-22 02:13

There is a test, smth like:

 import //needed imports

 public class TestClass{
    WebDriver driver;

    @Before
    public void setUp() {
       //some cod         


        
相关标签:
1条回答
  • 2020-11-22 02:55

    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()
      
    0 讨论(0)
提交回复
热议问题