背景
我設(shè)置了一個c#代碼來運(yùn)行Selenium chromedriver.exe.在運(yùn)行結(jié)束時,我有browser.close()來關(guān)閉實例屋摇。(browser = webdriver.Chrome())我相信它應(yīng)該從內(nèi)存中釋放chromedriver.exe(我在Windows 7上)失驶。但是每次運(yùn)行后矫渔,內(nèi)存中仍有一個chromedriver.exe實例啃奴。
問題窺探
從理論上講腊满,調(diào)用browser.Quit將關(guān)閉所有瀏覽器選項卡并終止進(jìn)程扯旷。
但是拯爽,在我的情況下,我無法做到這一點(diǎn) - 因為我并行運(yùn)行多個測試钧忽,我不想進(jìn)行一次測試來關(guān)閉其他人的窗口毯炮。因此,當(dāng)我的測試完成運(yùn)行時惰瓜,仍有許多“chromedriver.exe”進(jìn)程在運(yùn)行否副。
解決辦法
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) + " 結(jié)束-------------------");
? ? ? ? ? ? }
? ? ? ? }
在C#控制臺應(yīng)用程序中使用了chrome驅(qū)動程序崎坊,只有在將所有三種方法一起調(diào)用后才能清理延遲進(jìn)程备禀。