[英]Try Selenium Download use of Latest chromedriver.exe in C# if it Fails to Instanciate to fix Session not created Exception
In my Selenium UI tests that use the NuGet package Selenium.WebDriver I can already detect if I need to download a newer chromedriver.exe. 如果new ChromeDriver()
失敗:
try
{
this.driver = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, options);
}
catch (Exception ex)
{
throw new Exception($"Failed to setup chromedriver. Consider killing chromedriver.exe and downloading and updating it from https://chromedriver.chromium.org/downloads and update it in project with copy to output. Exception: {ex}");
}
異常消息是:
session 未創建:此版本的 ChromeDriver 僅支持 Chrome 版本 [版本]
此外,我已經可以通過檢查來檢測是否尚未下載 chromedriver:
var chromedriverPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "chromedriver.exe");
if(!File.Exists(chromedriverPath))
{
throw new Exception($"chromedriver.exe not found at location {chromedriverPath}. Download it from https://chromedriver.chromium.org/downloads and update it in project with copy to output");
}
我正在尋找一種推薦的方法來自動修復此錯誤。
類似的東西(在檢測到 chromedriver.exe 丟失或過時):
獎金:
如果它仍然失敗也沒關系,但它至少應該嘗試將 chromedriver.exe 更新到最新版本,因為這通常可以解決問題。 過去我嘗試過軟件包,但這些軟件包通常只是運送 chromedriver.exe 文件並且維護不善。 此外,它還需要是值得信賴的 package,並且有很多下載讓我再次考慮。 跨操作系統的事情也是一個話題。
如果您在這里寫了一個很好的答案,請考慮從中創建一個 package 或至少允許為此目的使用您的代碼:)
我發現 Niels Swimberghe @Swimburger https://github.com/Swimburger/DownloadChromeDriverSample的這個很棒的回購
這里是 class 的鏈接復制粘貼: https://raw.githubusercontent.com/Swimburger/DownloadChromeDriverSample/main/Console/ChromeDriverInstaller.cs
用法:
//autofixing chromedriver
var chromedriverInstaller = new ChromeDriverInstaller();
chromedriverInstaller.Install().Wait();
try
{
this.driver = new ChromeDriver(AppDomain.CurrentDomain.BaseDirectory, options);
}
catch (Exception ex)
{
throw new Exception($"Failed to setup chromedriver. Consider killing chromedriver.exe and downloading and updating it from https://chromedriver.chromium.org/downloads and update it in project with copy to output. Exception: {ex}");
}
它並不完美,因為理想情況下它只會在測試失敗時運行。 但接近完美:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.