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

心不动则不痛 提交于 2021-01-29 04:37:43

问题


There is a test, smth like:

 import //needed imports

 public class TestClass{
    WebDriver driver;

    @Before
    public void setUp() {
       //some code
    }

    @Test
    public void test1() {
       //some code, including init of driver (geckodriver)
    }

   //@After
   // public void tearDown() {
   //  driver.quit();
   //}
}

So, I inited geckodriver, and successfully running my tests, using firefox instances. But I want Not to close firefox window after each run, because I just want to analyse what I have, and fix any needed, after test run(I'm going to unComment driver.quit() later). At the same time, each calling without closing the driver leads to over-impact to RAM on my PC(and does not matter - did I close browser manually, or not, after test):

So, question is: is there any way to close the process(more precisely - do smth, which will close geckodriver.exe process in taskmgr) of "geckodriver", but will NOT close the browser after test finished? e.g., adding some method in test itself, whatever... This not impacts my work/test itself, I just want to add some optimizing.


回答1:


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


来源:https://stackoverflow.com/questions/65088336/seleniums-webdriver-is-not-clearing-memory-after-quit-with-parallel-webdriver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!