c# 從內(nèi)存中釋放Selenium chromedriver.exe
背景
我設(shè)置了一個c#代碼來運(yùn)行Selenium chromedriver.exe.在運(yùn)行結(jié)束時,我有browser.close()來關(guān)閉實(shí)例。(browser = webdriver.Chrome())我相信它應(yīng)該從內(nèi)存中釋放chromedriver.exe(我在Windows 7上)。但是每次運(yùn)行后,內(nèi)存中仍有一個chromedriver.exe實(shí)例。
問題窺探
從理論上講,調(diào)用browser.Quit將關(guān)閉所有瀏覽器選項(xiàng)卡并終止進(jìn)程。
但是,在我的情況下,我無法做到這一點(diǎn) - 因?yà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)程。
以上就是c# 從內(nèi)存中釋放Selenium chromedriver.exe的詳細(xì)內(nèi)容,更多關(guān)于c# 內(nèi)存中釋放Selenium 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#實(shí)現(xiàn)遞歸算法經(jīng)典實(shí)例
這篇文章主要為大家介紹了C#實(shí)現(xiàn)遞歸算法,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01
C#語法糖(Csharp Syntactic sugar)大匯總
首先需要聲明的是“語法糖”這個詞絕非貶義詞,它可以給我?guī)矸奖?,是一種便捷的寫法,編譯器會幫我們做轉(zhuǎn)換;而且可以提高開發(fā)編碼的效率,在性能上也不會帶來損失。這讓java開發(fā)人員羨慕不已,呵呵。2010-06-06
C# DataTable中查詢指定字段名稱的數(shù)據(jù)
這篇文章主要介紹了C# DataTable中查詢指定字段名稱的數(shù)據(jù),本文直接給出實(shí)例代碼,簡單易懂,需要的朋友可以參考下2015-06-06
C#動態(tài)生成DropDownList執(zhí)行失敗原因分析
這篇文章主要介紹了C#動態(tài)生成DropDownList執(zhí)行失敗原因分析,以一個實(shí)例形式分析了C#動態(tài)生成DropDownList的相關(guān)注意要點(diǎn)與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
Unity3D Shader實(shí)現(xiàn)貼圖切換效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)貼圖切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-03-03
MVC設(shè)定默認(rèn)路由為指定的Area下的某個action
今天小編就為大家分享一篇關(guān)于MVC設(shè)定默認(rèn)路由為指定的Area下的某個action,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-01-01
C#中使用Join與GroupJoin將兩個集合進(jìn)行關(guān)聯(lián)與分組
這篇文章主要介紹了C#中使用Join與GroupJoin將兩個集合進(jìn)行關(guān)聯(lián)與分組,文中分別對Join和GroupJoin的用法進(jìn)行詳細(xì)說明,需要的朋友可以參考下2017-12-12

