背景
我設置了一個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驅動程序,只有在將所有三種方法一起調用后才能清理延遲進程。