Skip to content

Instantly share code, notes, and snippets.

@Swimburger
Last active June 5, 2024 14:49
Show Gist options
  • Save Swimburger/aa5fd64151d84443f395f56f803b0ae2 to your computer and use it in GitHub Desktop.
Save Swimburger/aa5fd64151d84443f395f56f803b0ae2 to your computer and use it in GitHub Desktop.
Install ChromeDriver using PowerShell on Windows/Linux/MacOS, for more information read this blog post: https://swimburger.net/blog/powershell/download-the-right-chromedriver-on-windows-linux-macos-using-powershell
[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;
[CmdletBinding()]
param (
[Parameter(Mandatory = $True)]
[string]
$ChromeDriverOutputPath,
[Parameter(Mandatory = $True)]
[ValidateSet('linux64','mac-arm64', 'mac-x64', 'win32', 'win64')]
[string]
$Platform,
[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.";
}
}
If ([string]::IsNullOrEmpty($ChromeVersion)) {
$ChromeVersion = Get-ChromeVersion -ErrorAction Stop;
Write-Output "Google Chrome version $ChromeVersion found on machine";
}
$ChromeVersion = $ChromeVersion.Substring(0, $ChromeVersion.LastIndexOf("."));
$ChromeDriverVersion = Invoke-WebRequest "https://googlechromelabs.github.io/chrome-for-testing/LATEST_RELEASE_$ChromeVersion" `
-ContentType 'text/plain' |
Select-Object -ExpandProperty Content;
$ChromeDriverVersion = [System.Text.Encoding]::UTF8.GetString($ChromeDriverVersion);
Write-Output "Latest matching version of Chrome Driver is $ChromeDriverVersion";
$DownloadUrl = Invoke-WebRequest 'https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json'
| Select-Object -ExpandProperty Content
| ConvertFrom-Json
| Select-Object -ExpandProperty versions
| Where-Object { $_.version -eq $ChromeDriverVersion }
| Select-Object -ExpandProperty downloads
| Select-Object -ExpandProperty chromedriver
| Where-Object { $_.platform -eq $Platform }
| Select-Object -ExpandProperty url;
If (($ForceDownload -eq $False) -and (Test-path $ChromeDriverOutputPath)) {
$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", "");
Invoke-WebRequest $DownloadUrl -OutFile $TempZipFilePath;
Expand-Archive $TempZipFilePath -DestinationPath $TempFileUnzipPath;
Get-Childitem $TempFileUnzipPath -Recurse -File -Filter 'chromedriver*'
| Move-Item -Destination $ChromeDriverOutputPath -Force;
# Clean up temp files
Remove-Item $TempZipFilePath;
Remove-Item $TempFileUnzipPath -Recurse;
# reset back to original Progress Preference
$ProgressPreference = $OriginalProgressPreference;
@dashinghari6
Copy link

Hi,
Am trying to use this code for edge driver.
But am stuck at the below part .

How we can find the latest_release for edge like below.
IMG_20211109_181344__01

@Swimburger
Copy link
Author

@dashinghari6, the Microsoft Edge driver is not distributed by Google.
It is distributed by Microsoft and you can find it here: https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/
They don't use a similar system as Google AFAIK.

@GlennM
Copy link

GlennM commented Aug 21, 2023

Did Google change the endpoint since Chrome version 115?
Their documentation now points to https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json

Think this script may need an update now @Swimburger

@Swimburger
Copy link
Author

@GlennM I added an updated script.
You can use it like this: ./InstallChromeDriver.ps1 -OutputPath ./chromedriver.exe -Platform win32
On non Windows machines you'd not add an extension to the chromedriver:
./InstallChromeDriver.ps1 -OutputPath ./chromedriver -Platform mac-arm64

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment