背景
我設(shè)置了一個(gè)c#代碼來(lái)運(yùn)行Selenium chromedriver.exe.在運(yùn)行結(jié)束時(shí),我有browser.close()來(lái)關(guān)閉實(shí)例。(browser = webdriver.Chrome())我相信它應(yīng)該從內(nèi)存中釋放chromedriver.exe(我在Windows 7上)。但是每次運(yùn)行后,內(nèi)存中仍有一個(gè)chromedriver.exe實(shí)例。
問(wèn)題窺探
從理論上講,調(diào)用browser.Quit將關(guān)閉所有瀏覽器選項(xiàng)卡并終止進(jìn)程。
但是,在我的情況下,我無(wú)法做到這一點(diǎn) - 因?yàn)槲也⑿羞\(yùn)行多個(gè)測(cè)試,我不想進(jìn)行一次測(cè)試來(lái)關(guān)閉其他人的窗口。因此,當(dāng)我的測(cè)試完成運(yùn)行時(shí),仍有許多“chromedriver.exe”進(jìn)程在運(yùn)行。
解決辦法
public override void DoJob(IJobExecutionContext context, ILifetimeScope scope, string[] args)
? ? ? ? {
? ? ? ? ? ? Console.WriteLine(nameof(LoginReptiles1688Job) + " 開(kāi)始-------------------");
? ? ? ? ? ? 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??刂婆_(tái)應(yīng)用程序中使用了chrome驅(qū)動(dòng)程序,只有在將所有三種方法一起調(diào)用后才能清理延遲進(jìn)程。