c# 從內(nèi)存中釋放Selenium chromedriver.exe
背景
我設(shè)置了一個c#代碼來運行Selenium chromedriver.exe.在運行結(jié)束時,我有browser.close()來關(guān)閉實例。(browser = webdriver.Chrome())我相信它應(yīng)該從內(nèi)存中釋放chromedriver.exe(我在Windows 7上)。但是每次運行后,內(nèi)存中仍有一個chromedriver.exe實例。
問題窺探
從理論上講,調(diào)用browser.Quit將關(guān)閉所有瀏覽器選項卡并終止進程。
但是,在我的情況下,我無法做到這一點 - 因為我并行運行多個測試,我不想進行一次測試來關(guān)閉其他人的窗口。因此,當(dāng)我的測試完成運行時,仍有許多“chromedriver.exe”進程在運行。
解決辦法
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)用后才能清理延遲進程。
以上就是c# 從內(nèi)存中釋放Selenium chromedriver.exe的詳細內(nèi)容,更多關(guān)于c# 內(nèi)存中釋放Selenium 的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C#客戶端程序調(diào)用外部程序的3種實現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#客戶端程序調(diào)用外部程序的3種實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-04-04
C#簡單實現(xiàn)表達式目錄樹(Expression)
表達式目錄樹以數(shù)據(jù)形式表示語言級別代碼。數(shù)據(jù)存儲在樹形結(jié)構(gòu)中。表達式目錄樹中的每個節(jié)點都表示一個表達式。這篇文章給大家介紹C#簡單實現(xiàn)表達式目錄樹(Expression),需要的朋友參考下吧2017-11-11
C# listview添加combobox到單元格的實現(xiàn)代碼
從別處轉(zhuǎn)來的,自己進行了一些小的修改,還不錯,你自己先拖一個ListView1和一個ComboBox1,需要的朋友可以參考下2014-06-06

