如何在Selenium中释放和释放封闭的Chrome浏览器的记忆?

时间:2019-01-04 18:07:06

标签: c# selenium google-chrome selenium-webdriver selenium-chromedriver

我编写了一个多线程自动化程序,它使用了许多多个带有Selenium和C#的Chrome浏览器(可以重复)

我尝试使用所有内容,包括“-隐身”,“-disable-application-cache”,等一秒钟再打开另一个浏览器driver.Close().Quit().Dispose() 。这是代码示例:

IWebDriver[] drivers = new IWebDriver[AUTOMATION_NUM];
int QUEUE_BROWSERS_TO_OPEN = 0;

Thread mainThread = new Thread(() =>
{
    for (int i = 0; i < AUTOMATION_NUM && !EXIT; i++)
    {
        ...
        Thread driverThread = new Thread(() => {
            ...
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("--incognito", "--disable-application-cache"); // headless or non-headless

            IWebDriver driver = drivers[instance];
            driver = new ChromeDriver(chromeService, chromeOptions, TIMEOUT_FROM);
            ...
            ++QUEUE_BROWSERS_TO_OPEN;
            ...
            driver.Close();
            driver.Quit();
            driver.Dispose();
        });

        _machineThreads.Add(driverThread);
        driverThread.IsBackground = true;
        driverThread.Start();

        while (i < AUTOMATION_NUM && !EXIT)
        {
            Thread.Sleep(5000);

            if (QUEUE_BROWSERS_TO_OPEN > 0 && BROWSERS_OPEN < BROWSER_NUM)
                --QUEUE_BROWSERS_TO_OPEN;
        }
    }

    Thread endThread = new Thread(() =>
    {
        foreach (Thread machineThread in _machineThreads)
            machineThread.Join();

        ...
    });
    endThread.Start();
});
mainThread.Start();

并建议我的客户端通过单击按钮执行以下代码来杀死所有“ chromedriver.exe”进程:

Process[] chromeDriverProcesses = Process.GetProcessesByName("chromedriver");

foreach (var chromeDriverProcess in chromeDriverProcesses)
    chromeDriverProcess.Kill();

但是我的客户仍然说封闭的和完成的自动浏览器的记忆仍然存在吗?

Closed Chrome browsers' memories

任何可行的解决方案如何在重新使用自动化之前防止客户端的高端PC重新启动以释放Chrome内存或冻结?还有其他选择吗?

1 个答案:

答案 0 :(得分:1)

有命令杀死所有打开的浏览器窗口(chrome.exe)和与该浏览器窗口相关联的chrome驱动程序进程(chromedriver.exe)

杀死Chrome驱动程序进程

string strCmdText;
strCmdText= "TASKKILL /f  /IM  CHROMEDRIVER.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

杀死Chrome浏览器窗口

string strCmdText;
strCmdText= "TASKKILL /f  /IM  CHROME.EXE";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);

有关更多信息,请参见thisthis
希望这对您有帮助...