Download the right ChromeDriver version & keep it up to date on Windows/Linux/macOS using PowerShell

PowerShell logo and Google Chrome logo next to title: Download the right ChromeDriver version using PowerShell
Unhandled exception. System.InvalidOperationException: session not created: This version of ChromeDriver only supports Chrome version 74
(Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.19042 x86_64) (SessionNotCreated)
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor()
at SeleniumConsole.Program.Main(String[] args) in C:\Users\niels\source\repos\SeleniumConsole\Program.cs:line 10

How to download the correct version of Chrome

Build a PowerShell script to install the correct ChromeDriver

(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe').'(Default)').VersionInfo.FileVersion
Try{
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' -ErrorAction Stop).'(Default)').VersionInfo.FileVersion;
}Catch{
Throw "Google Chrome not found in registry";
}
# $IsWindows will PowerShell Core but not on PowerShell 5 and below, but $Env:OS does
# this way you can safely check whether the current machine is running Windows pre and post PowerShell Core
If ($IsWindows -or $Env:OS) {

}
Try{
# this will check whether google-chrome command is available
Get-Command google-chrome -ErrorAction Stop | Out-Null;
google-chrome --product-version;
}Catch{
Throw "'google-chrome' command not found";
}
ElseIf ($IsLinux) {

}
$ChromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
If (Test-Path $ChromePath) {
$Version = & $ChromePath --version;
$Version = $Version.Replace("Google Chrome ", "");
$Version;
}
Else {
Throw "Google Chrome not found on your MacOS machine";
}
ElseIf ($IsMacOS) {

}
Else {
Throw "Your operating system is not supported by this script.";
}
Function Get-ChromeVersion {
# $IsWindows will PowerShell Core but not on PowerShell 5 and below, but $Env:OS does
# this way you can safely check whether the current machine is running Windows pre and post PowerShell Core
If ($IsWindows -or $Env:OS) {
Try {
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' -ErrorAction Stop).'(Default)').VersionInfo.FileVersion;
}
Catch {
Throw "Google Chrome not found in registry";
}
}
ElseIf ($IsLinux) {
Try {
# this will check whether google-chrome command is available
Get-Command google-chrome -ErrorAction Stop | Out-Null;
google-chrome --product-version;
}
Catch {
Throw "'google-chrome' command not found";
}
}
ElseIf ($IsMacOS) {
$ChromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
If (Test-Path $ChromePath) {
$Version = & $ChromePath --version;
$Version = $Version.Replace("Google Chrome ", "");
$Version;
}
Else {
Throw "Google Chrome not found on your MacOS machine";
}
}
Else {
Throw "Your operating system is not supported by this script.";
}
}
# Instructions from https://chromedriver.chromium.org/downloads/version-selection
# First, find out which version of Chrome you are using. Let's say you have Chrome 72.0.3626.81.
$ChromeVersion = Get-ChromeVersion -ErrorAction Stop;
Write-Output "Google Chrome version $ChromeVersion found on machine";
#   Take the Chrome version number, remove the last part, 
$ChromeVersion = $ChromeVersion.Substring(0, $ChromeVersion.LastIndexOf("."));
# and append the result to URL "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_".
# For example, with Chrome version 72.0.3626.81, you'd get a URL "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_72.0.3626".
$ChromeDriverVersion = (Invoke-WebRequest "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$ChromeVersion").Content;
Write-Output "Latest matching version of Chrome Driver is $ChromeDriverVersion";
$TempFilePath = [System.IO.Path]::GetTempFileName();
$TempZipFilePath = $TempFilePath.Replace(".tmp", ".zip");
Rename-Item -Path $TempFilePath -NewName $TempZipFilePath;
$TempFileUnzipPath = $TempFilePath.Replace(".tmp", "");
#   Use the URL created in the last step to retrieve a small file containing the version of ChromeDriver to use. For example, the above URL will get your a file containing "72.0.3626.69". (The actual number may change in the future, of course.)
# Use the version number retrieved from the previous step to construct the URL to download ChromeDriver. With version 72.0.3626.69, the URL would be "https://chromedriver.storage.googleapis.com/index.html?path=72.0.3626.69/".
If ($IsWindows -or $Env:OS) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_win32.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver.exe" -Destination "path/to/save/chromedriver.exe" -Force;
}
ElseIf ($IsLinux) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_linux64.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver" -Destination "path/to/save/chromedriver" -Force;
}
ElseIf ($IsMacOS) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_mac64.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver" -Destination "path/to/save/chromedriver" -Force;
}
Else {
Throw "Your operating system is not supported by this script.";
}
# Clean up temp files
Remove-Item $TempZipFilePath;
Remove-Item $TempFileUnzipPath -Recurse;

End result: Enhanced PowerShell script to download ChromeDriver

[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[string]
$ChromeDriverOutputPath,
[Parameter(Mandatory = $false)]
[string]
$ChromeVersion,
[Parameter(Mandatory = $false)]
[Switch]
$ForceDownload
)

# store original preference to revert back later
$OriginalProgressPreference = $ProgressPreference;
# setting progress preference to silently continue will massively increase the performance of downloading the ChromeDriver
$ProgressPreference = 'SilentlyContinue';

Function Get-ChromeVersion {
# $IsWindows will PowerShell Core but not on PowerShell 5 and below, but $Env:OS does
# this way you can safely check whether the current machine is running Windows pre and post PowerShell Core
If ($IsWindows -or $Env:OS) {
Try {
(Get-Item (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\chrome.exe' -ErrorAction Stop).'(Default)').VersionInfo.FileVersion;
}
Catch {
Throw "Google Chrome not found in registry";
}
}
ElseIf ($IsLinux) {
Try {
# this will check whether google-chrome command is available
Get-Command google-chrome -ErrorAction Stop | Out-Null;
google-chrome --product-version;
}
Catch {
Throw "'google-chrome' command not found";
}
}
ElseIf ($IsMacOS) {
$ChromePath = '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome';
If (Test-Path $ChromePath) {
$Version = & $ChromePath --version;
$Version = $Version.Replace("Google Chrome ", "");
$Version;
}
Else {
Throw "Google Chrome not found on your MacOS machine";
}
}
Else {
Throw "Your operating system is not supported by this script.";
}
}

# Instructions from https://chromedriver.chromium.org/downloads/version-selection
# First, find out which version of Chrome you are using. Let's say you have Chrome 72.0.3626.81.
If ([string]::IsNullOrEmpty($ChromeVersion)) {
$ChromeVersion = Get-ChromeVersion -ErrorAction Stop;
Write-Output "Google Chrome version $ChromeVersion found on machine";
}

# Take the Chrome version number, remove the last part,
$ChromeVersion = $ChromeVersion.Substring(0, $ChromeVersion.LastIndexOf("."));
# and append the result to URL "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_".
# For example, with Chrome version 72.0.3626.81, you'd get a URL "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_72.0.3626".
$ChromeDriverVersion = (Invoke-WebRequest "https://chromedriver.storage.googleapis.com/LATEST_RELEASE_$ChromeVersion").Content;
Write-Output "Latest matching version of Chrome Driver is $ChromeDriverVersion";

If (($ForceDownload -eq $False) -and (Test-path $ChromeDriverOutputPath)) {
#ChromeDriver 88.0.4324.96 (68dba2d8a0b149a1d3afac56fa74648032bcf46b-refs/branch-heads/4324@{#1784})
$ExistingChromeDriverVersion = & $ChromeDriverOutputPath --version;
$ExistingChromeDriverVersion = $ExistingChromeDriverVersion.Split(" ")[1];
If ($ChromeDriverVersion -eq $ExistingChromeDriverVersion) {
Write-Output "Chromedriver on machine is already latest version. Skipping.";
Write-Output "Use -ForceDownload to reinstall regardless";
Exit;
}
}

$TempFilePath = [System.IO.Path]::GetTempFileName();
$TempZipFilePath = $TempFilePath.Replace(".tmp", ".zip");
Rename-Item -Path $TempFilePath -NewName $TempZipFilePath;
$TempFileUnzipPath = $TempFilePath.Replace(".tmp", "");
# Use the URL created in the last step to retrieve a small file containing the version of ChromeDriver to use. For example, the above URL will get your a file containing "72.0.3626.69". (The actual number may change in the future, of course.)
# Use the version number retrieved from the previous step to construct the URL to download ChromeDriver. With version 72.0.3626.69, the URL would be "https://chromedriver.storage.googleapis.com/index.html?path=72.0.3626.69/".

If ($IsWindows -or $Env:OS) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_win32.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver.exe" -Destination $ChromeDriverOutputPath -Force;
}
ElseIf ($IsLinux) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_linux64.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver" -Destination $ChromeDriverOutputPath -Force;
}
ElseIf ($IsMacOS) {
Invoke-WebRequest "https://chromedriver.storage.googleapis.com/$ChromeDriverVersion/chromedriver_mac64.zip" -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Move-Item "$TempFileUnzipPath/chromedriver" -Destination $ChromeDriverOutputPath -Force;
}
Else {
Throw "Your operating system is not supported by this script.";
}

# After the initial download, it is recommended that you occasionally go through the above process again to see if there are any bug fix releases.

# Clean up temp files
Remove-Item $TempZipFilePath;
Remove-Item $TempFileUnzipPath -Recurse;

# reset back to original Progress Preference
$ProgressPreference = $OriginalProgressPreference;
.\InstallChromeDriver.ps1 -ChromeDriverOutputPath .\chromedriver.exe
.\InstallChromeDriver.ps1 -ChromeDriverOutputPath .\chromedriver.exe -ChromeVersion 88.0.4324.182
.\InstallChromeDriver.ps1 -ChromeDriverOutputPath .\chromedriver.exe -ForceDownload

Swimburger is a Belgian .NET web developer and technical content creator at http://swimburger.net

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store