背景
我設定了一個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驅動程式,只有在將所有三種方法一起呼叫后才能清理延遲行程,
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/247012.html
標籤:.NET技术
上一篇:C#初學,資料型別
