Selenium:如何在不调用driver.quit()的情况下停止影响PC内存的geckodriver进程? [英] Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?

查看:201
本文介绍了Selenium:如何在不调用driver.quit()的情况下停止影响PC内存的geckodriver进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个测试,如:

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

所以,我参加了geckodriver,并成功运行我的测试,使用firefox实例。
但是我想不要在每次运行后关闭firefox窗口,因为我只是想分析我拥有的东西,并在测试运行后修复所需的东西(我将在稍后的unComment driver.quit())。
同时,每次调用而不关闭驱动程序会导致我的PC上的RAM过度影响(并且无关紧要 - 我是否手动关闭浏览器,测试后没有关闭):

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

所以,问题是:
有没有办法关闭这个过程(更确切地说 - 做smth,哪个将关闭geckodriver的taskmgr中的geckodriver.exe进程,但测试完成后不会关闭浏览器?例如,在测试中添加一些方法,无论如何......这不会影响我的工作/测试本身,我只想添加一些优化。

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.

推荐答案

根据你的问题注释掉 driver.quit() 只是不要在每次关闭后关闭firefox窗口运行,因为我只想分析我的内容不会成为最佳实践的一部分。

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.

任何详细分析我们可以创建日志条目并拍摄快照。

通过 Selenium自动执行 按照最佳做法,您应该在 tearDown(){}中调用 quit() 方法code>。通过发送退出调用 退出() 删除 s当前浏览会话命令, {flags:[eForceQuit]} ,最后在 / shutdown <$上发送 GET 请求C $ C>端点。以下是一个示例:

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

所以在调用 quit() 方法 Web浏览器会话和 WebDriver 实例完全被杀死。因此,您不必包含任何额外的开销步骤。

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.

如果你想执行杀死悬空的 WebDriver 实例,例如 GeckoDriver.exe 实例您可以使用以下任一代码块来杀死任何悬空的 WebDriver 实例:

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解决方案(特定于WindowsOS ):

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解决方案(特定于WindowsOS ):

    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解决方案(跨平台):

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


  • 这篇关于Selenium:如何在不调用driver.quit()的情况下停止影响PC内存的geckodriver进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆