c# 從內(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) + " 開始-------------------");
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)程。
以上就是c# 從內(nèi)存中釋放Selenium chromedriver.exe的詳細(xì)內(nèi)容,更多關(guān)于c# 內(nèi)存中釋放Selenium 的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(Expression)
表達(dá)式目錄樹以數(shù)據(jù)形式表示語(yǔ)言級(jí)別代碼。數(shù)據(jù)存儲(chǔ)在樹形結(jié)構(gòu)中。表達(dá)式目錄樹中的每個(gè)節(jié)點(diǎn)都表示一個(gè)表達(dá)式。這篇文章給大家介紹C#簡(jiǎn)單實(shí)現(xiàn)表達(dá)式目錄樹(Expression),需要的朋友參考下吧2017-11-11
C#實(shí)現(xiàn)將批量圖片轉(zhuǎn)為PDF文件
這篇文章主要為大家詳細(xì)介紹了如何使用 iTextSharp 庫(kù)實(shí)現(xiàn),將指定目錄下的有序的一組圖片,組合生成指定文件名的PDF文件,有需要的可以了解下2024-10-10
C#實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
給大家分享用C#寫出一個(gè)計(jì)算機(jī)功能的全部代碼分享,有興趣的朋友可以跟著做一下。2018-03-03
C#設(shè)置或驗(yàn)證PDF文本域格式的方法詳解
PDF中的文本域可以通過(guò)設(shè)置不同格式,用于顯示數(shù)字、貨幣、日期、時(shí)間、郵政編碼、電話號(hào)碼和社保號(hào)等等。本文將介紹如何通過(guò)C#設(shè)置或驗(yàn)證PDF文本域格式,需要的可以參考一下2022-01-01
C# listview添加combobox到單元格的實(shí)現(xiàn)代碼
從別處轉(zhuǎn)來(lái)的,自己進(jìn)行了一些小的修改,還不錯(cuò),你自己先拖一個(gè)ListView1和一個(gè)ComboBox1,需要的朋友可以參考下2014-06-06

