从内存中释放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 windowdriver?.Quit(); // Close the console app that was used to kick off the chrome windowdriver?.Dispose(); // Close the chromedriver.exedriver = null;options = null;detailtry = 0;shoptry = 0;Console.WriteLine(nameof(LoginReptiles1688Job) + " 結束-------------------");}}在C#控制臺應用程序中使用了chrome驅動程序,只有在將所有三種方法一起調用后才能清理延遲進程。
總結
以上是生活随笔為你收集整理的从内存中释放Selenium chromedriver.exe的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何在 C# 中使用 MSMQ
- 下一篇: 【日常排雷】 .Net core 生产环