How to autoupdate chromeDriver & geckDriver in selenium How to autoupdate chromeDriver & geckDriver in selenium selenium selenium

How to autoupdate chromeDriver & geckDriver in selenium


I would argue that your current approach is not a feasible approach. New versions of browsers are released with zero consideration for Selenium (or any other drivers). As soon as a new browser update is released, there is a reasonably high chance that there will be NO existing driver that works with that version. It often takes days for Selenium teams to release updated drivers to match the newest version of a browser.

And since you are automatically updating your browsers, then you are possibly automatically breaking your Selenium tests until a new driver version is released, or until you downgrade the browsers.

Now, you may be fine with that, and are okay with disabling a browser's tests until the most current Selenium drivers work with the most current browser version. If that is the case, then here are some solutions:

1) If you are using C#, store your drivers in the test solution as a Nuget package, or in a dependencies folder. Then, have the automation reference that driver no matter where it is running. When you need to update the driver, you literally only need to update it in one place, and check in the changes. All client machines will, through your CI process, pull down the latest code, which includes that new driver.

2) If for some reason you do not want the driver in your project as a Nuget package or a manually-saved dependency, then have your CI handle the update process. Point your automation code to a driver located in some common directory on whatever client machine it is currently running on -> wherever your machine stores the dependencies after downloading them. For example; downloading selenium files via console on a Windows machine will put them somewhere in %APPDATA% "C:\Users\xxxxxx\AppData\Roaming\npm\node_modules". That is where your test solution should look.

Then, in your CI scripts, before running any tests, download the latest driver. The syntax is nearly the same, if not identical between Windows and Linux/Unix Kernels. This assumes you have npm installed.

npm install -g selenium

If you already have latest, then nothing will happen. If you don't the latest driver will be downloaded by your CI script before running tests. Then, your test solution will be pointing to where the driver is stored on the client, and it will automatically be using the newest driver.


First @Asyranok is right, even when implemented auto update code will not work 100% of the time. However, for many of us, this occasional downtime is "ok" as long as it's just a few days.

I've found that manually updating X servers every few months to be incredibly irritating and while there are well written instructions on the selenium website on how to "auto update" the driver I've yet to see one openly available non-library implementation of this guide.

My answer is specific to C#, for this language the solution typically suggested is to use NuGet to pull the latest driver automatically, this has two issues:

  1. You need to deploy at the frequency of chrome updating (most companies aren't there yet, neither are we) or your application will be "broken" for the time between chrome updating and your "new" version of the application deploying, and again this is only if you release on a schedule, if you release ad-hoc your going to have to go through a series of manual steps to update, build, release etc. to get your application working again.

  2. You need (typically, without a work around) to pull the latest chromedrive from NuGet by hand, again a manual process.

What would be nice would be what python has and @leminhnguyenHUST suggests which is using a library that will automatically pull the latest chromedriver on runtime. I've looked around and haven't yet found anything for C# that does this, so I decided to roll my own and build that into my application:

public void DownloadLatestVersionOfChromeDriver(){    string path = DownloadLatestVersionOfChromeDriverGetVersionPath();    var version = DownloadLatestVersionOfChromeDriverGetChromeVersion(path);    var urlToDownload = DownloadLatestVersionOfChromeDriverGetURLToDownload(version);    DownloadLatestVersionOfChromeDriverKillAllChromeDriverProcesses();    DownloadLatestVersionOfChromeDriverDownloadNewVersionOfChrome(urlToDownload);}public string DownloadLatestVersionOfChromeDriverGetVersionPath(){    //Path originates from here: https://chromedriver.chromium.org/downloads/version-selection                using (RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\chrome.exe"))    {        if (key != null)        {            Object o = key.GetValue("");            if (!String.IsNullOrEmpty(o.ToString()))            {                return o.ToString();            }            else            {                throw new ArgumentException("Unable to get version because chrome registry value was null");            }        }        else        {            throw new ArgumentException("Unable to get version because chrome registry path was null");        }    }}public string DownloadLatestVersionOfChromeDriverGetChromeVersion(string productVersionPath){    if (String.IsNullOrEmpty(productVersionPath))    {        throw new ArgumentException("Unable to get version because path is empty");    }    if (!File.Exists(productVersionPath))    {        throw new FileNotFoundException("Unable to get version because path specifies a file that does not exists");    }    var versionInfo = FileVersionInfo.GetVersionInfo(productVersionPath);    if (versionInfo != null && !String.IsNullOrEmpty(versionInfo.FileVersion))    {        return versionInfo.FileVersion;    }    else    {        throw new ArgumentException("Unable to get version from path because the version is either null or empty: " + productVersionPath);    }}public string DownloadLatestVersionOfChromeDriverGetURLToDownload(string version){    if (String.IsNullOrEmpty(version))    {        throw new ArgumentException("Unable to get url because version is empty");    }    //URL's originates from here: https://chromedriver.chromium.org/downloads/version-selection    string html = string.Empty;    string urlToPathLocation = @"https://chromedriver.storage.googleapis.com/LATEST_RELEASE_" + String.Join(".", version.Split('.').Take(3));    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToPathLocation);    request.AutomaticDecompression = DecompressionMethods.GZip;    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())    using (Stream stream = response.GetResponseStream())    using (StreamReader reader = new StreamReader(stream))    {        html = reader.ReadToEnd();    }    if (String.IsNullOrEmpty(html))    {        throw new WebException("Unable to get version path from website");    }    return "https://chromedriver.storage.googleapis.com/" + html + "/chromedriver_win32.zip";}public void DownloadLatestVersionOfChromeDriverKillAllChromeDriverProcesses(){    //It's important to kill all processes before attempting to replace the chrome driver, because if you do not you may still have file locks left over    var processes = Process.GetProcessesByName("chromedriver");    foreach (var process in processes)    {        try        {            process.Kill();        }        catch        {            //We do our best here but if another user account is running the chrome driver we may not be able to kill it unless we run from a elevated user account + various other reasons we don't care about        }    }}public void DownloadLatestVersionOfChromeDriverDownloadNewVersionOfChrome(string urlToDownload){    if (String.IsNullOrEmpty(urlToDownload))    {        throw new ArgumentException("Unable to get url because urlToDownload is empty");    }    //Downloaded files always come as a zip, we need to do a bit of switching around to get everything in the right place    using (var client = new WebClient())    {        if (File.Exists(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.zip"))        {            File.Delete(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.zip");        }        client.DownloadFile(urlToDownload, "chromedriver.zip");        if (File.Exists(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.zip") && File.Exists(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.exe"))        {            File.Delete(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.exe");        }        if (File.Exists(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.zip"))        {            System.IO.Compression.ZipFile.ExtractToDirectory(System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\chromedriver.zip", System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location));        }    }}

Then usually I'll stick this very hacky invocation at the beginning of my application to invoke this feature and ensure that the latest chromedriver is available for my application:

//This is a very poor way of determining if I "need" to update the chromedriver,     //however I've yet to figure out a better way of doing this...try{    using (var chromeDriver = SetupChromeDriver())    {        chromeDriver.Navigate().GoToUrl("www.google.com");        chromeDriver.Quit();    }}catch{    DownloadLatestVersionOfChromeDriver();}

I'm sure this could be improved significantly, but so far it's worked for me.

Note: Cross Posted Here


I know its a bit of an old question, but I thought this could also be helpful to people: https://github.com/rosolko/WebDriverManager.Net. Its a Nuget package (https://www.nuget.org/packages/WebDriverManager/) that appears to solve the problem using .NET.