從內(nèi)存中釋放Selenium chromedriver.exe

背景

我設(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)程。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容