1. 程式人生 > 實用技巧 >從記憶體中釋放Selenium chromedriver.exe

從記憶體中釋放Selenium chromedriver.exe

背景

我設定了一個c#程式碼來執行Selenium chromedriver.exe.在執行結束時,我有browser.close()來關閉例項。(browser = webdriver.Chrome())我相信它應該從記憶體中釋放chromedriver.exe(我在Windows 7上)。但是每次執行後,記憶體中仍有一個chromedriver.exe例項。

問題窺探

從理論上講,呼叫browser.Quit將關閉所有瀏覽器選項卡並終止程序。

但是,在我的情況下,我無法做到這一點 - 因為我並行執行多個測試,我不想進行一次測試來關閉其他人的視窗。因此,當我的測試完成執行時,仍有許多“chromedriver.exe”程序在執行。

解決辦法

   public override void DoJob(IJobExecutionContext context, ILifetimeScope scope, string[] args)
        {
            Console.WriteLine(nameof(LoginReptiles1688Job) + " 開始-------------------");
            ChromeOptions options = null;
            IWebDriver driver = null;
            try
            {
                options 
= new ChromeOptions(); options.AddArguments("--ignore-certificate-errors"); options.AddArguments("--ignore-ssl-errors"); var listCookie = CookieHelp.GetCookie(); if (listCookie != null) { // options.AddArgument("headless");
} ChromeDriverService service = ChromeDriverService.CreateDefaultService(System.Environment.CurrentDirectory); service.HideCommandPromptWindow = true; driver = new ChromeDriver(service, options, TimeSpan.FromSeconds(120)); var setLoginStatus = scope.Resolve<ISetLoginStatus>(); IReptilesImageSearchService _reptilesImageSearchService = scope.Resolve<IReptilesImageSearchService>(); CrawlingWeb(_reptilesImageSearchService, driver); CrawlingWebShop(_reptilesImageSearchService, driver); } catch (Exception ex) { throw ex; } 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 driver = null; options = null; detailtry = 0; shoptry = 0; Console.WriteLine(nameof(LoginReptiles1688Job) + " 結束-------------------"); } }

在C#控制檯應用程式中使用了chrome驅動程式,只有在將所有三種方法一起呼叫後才能清理延遲程序。