background
I set up a c# code to run Selenium chromedriver.exe. At the end of the run, I have browser.close() to close the instance. (browser = webDriver.chrome ()) I believe it should free ChromeDriver.exe from memory (I’m on Windows 7). However, there is still an instance of ChromeDriver.exe in memory after each run.
Problem to spy out
In theory, calling browser.quit will close all browser tabs and terminate the process.
But, in my case, I couldn’t do that – because I was running multiple tests in parallel, I didn’t want to close someone else’s window by running a test. So when my tests finished running, there were still many chromedriver.exe processes running.
The solution
public override void DoJob(IJobExecutionContext context, ILifetimeScope scope, {string [] args) Console. WriteLine (nameof (LoginReptiles1688Job) + "start -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --"); 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) + "-- -- -- -- -- -- -- -- -- end -- -- -- -- -- -- -- -- -- --"); }}Copy the code
The chrome driver used in the C# console application cleans up the delayed process only after all three methods are called together.