简体   繁体   中英

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. It is if new ChromeDriver() fails:

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}");
}

The Exception Message is:

session not created: This version of ChromeDriver only supports Chrome version [version]

Also I can already detect if chromedriver wasn't yet downloaded simply by checkcing:

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");
}

I am looking for an recommended way to fix this error automatically.

Something like (after detecting chromedriver.exe missing or outdated):

  1. Find out what latest version is and kill dead chromedriver.exe process
  2. Download and unpack eg from https://chromedriver.chromium.org/downloads
  3. Replace chromedriver.exe in AppDomain.CurrentDomain.BaseDirectory

Bonus:

  • The Exception message mentions a major version of chromedriver where there could be a fallback to use this version if latests also fails.
  • Detect your current OS an download the right package per OS. Especially interested in compatibility with macOS

It's ok if it then still fails but it should at least try to just update chromedriver.exe to latest because that usually fixes the problem. In the past I've tried packages but often those packages were just shipping the chromedriver.exe file and were badly maintained. Also it would need to be a trusted package with many downloads for me consider it again. Also the cross OS thing is a topic there.

If you write a great answer here consider creating a package out of it or at least allowing to use your code for that purpose:)

I found this great repo by Niels Swimberghe @Swimburger https://github.com/Swimburger/DownloadChromeDriverSample

Here a link to the class to copy paste: https://raw.githubusercontent.com/Swimburger/DownloadChromeDriverSample/main/Console/ChromeDriverInstaller.cs

Usage:

//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}");
}

It is not perfect because ideally it would only run if the test fails. But close to perfect:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM