Keep web browser and web driver version in Sync for Selenium

Publish Date: March 11, 2022

Keep web browser and web driver version in Sync for Selenium

Selenium is an open-source automation testing tool which uses web drivers to control the web browser. Web driver is an API developed and maintained by the respective web browser vendors. Selenium uses these web drivers for automation testing – but the important thing it requires is the web browser version and the web driver version to be the same.

When a new version of web browser is released, a respective version of web driver is also released at the same time. All the major web browsers (such as Google Chrome, Microsoft Edge, Mozilla Firefox etc.) get a release every once in a while. The thing with web browsers is that they are automatically updated however, the web driver needs to be updated manually. In a nutshell, Selenium requires you to keep the web browser version and web driver version in sync. If they both go out of sync, the automation scripts stop working.

A few admins tend to solve this issue by disabling the automatic updates for web browsers – which is not a good practice at all. The updates are released for a reason – these updates fix certain bugs, security vulnerabilities and even adds new features. If you find yourself into this situation, you’re on the right page.

I created a PowerShell script that keeps the web browser and web driver versions in Sync. You can schedule this script on the systems where Selenium is used for automation testing.

Requirements

The script requires Windows PowerShell version 5.1 (available with the latest version of Windows 10 and Windows 11).

The Script

Here is the script. Please note that I created this script for Microsoft Edge, but you can modify it to work with any other web browser of your choice. You just have to figure out the web driver download URL for your web browser.

<# .SYNOPSIS Web browser and web driver version Sync for Selenium .NOTES Author : Surender Kumar Author URI : https://www.techtutsonline.com/staff/surender-kumar/ Version : 1.0 Purpose : Web browser and web driver version Sync for Selenium #>

# set the path of web browser executable
$browser = Get-Item "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
$browserVersion = [version]($browser).VersionInfo.ProductVersion

# define the web driver download url 
$updateURI = "https://msedgedriver.azureedge.net/$browserVersion/edgedriver_win64.zip"

# define the current web driver location
$outPath = " D:\SELENIUM\drivers\edge_driver"
$driverFile = "msedgedriver.exe"
$webDriver = "$outPath\$driverFile"

# define the log settings
$logName = "Application"
$logSource = "WebDriver Update"
$EvtIDOK = 5050
$EvtIDError = 5051

# create log source if it doesn't exist
if ([System.Diagnostics.EventLog]::SourceExists($logSource) -eq $False) {
    New-EventLog -LogName $logName -Source $logSource
}

# determining the current web driver version
$webDriverVersion = [version](Get-Item $webDriver -ea 0).VersionInfo.ProductVersion
$webDriverVersionStr = (Get-Item $webDriver -ea 0).VersionInfo.ProductVersion

# comparing the version of web browser and web driver
if ($browserVersion -eq $webDriverVersion){
    Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web browser and web drivers are already running on same version ($webDriverVersionStr)..."
}else {
    # check and kill the web driver process if running
    $webDriverPs = Get-Process "msedgedriver" -ea 0
    if ($webDriverPs) {
      $webDriverPs | Stop-Process -Force
     }
     
    # downloading the updated driver zip file if versions do not match
    Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web browser and web driver versions are not same. Downloading the updated drivers..."
    Invoke-WebRequest $updateURI -OutFile "$outPath\webDriver.zip"
    $webDriverZip = "$outPath\webDriver.zip"
    if($webDriverZip){
        # extracting the web driver executable from zip archive
        Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Extracting the updated web driver executables..."
        Expand-Archive -Path $webDriverZip -DestinationPath $outPath -Force
    }
    # determining the updated web drivers version
    $driverFile1 = "msedgedriver.exe"
    $webDriver1 = "$outPath\$driverFile1"
    $webDriverVersion1 = [version](Get-Item $webDriver1 -ea 0).VersionInfo.ProductVersion
    $webDriverVersion1Str = (Get-Item $webDriver1 -ea 0).VersionInfo.ProductVersion
    if ($webDriverVersion1 -eq $browserVersion) {
        Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Web drivers were sucessfully updated to version ($webDriverVersion1Str)..."
    } else {
        Write-EventLog -LogName $logName -Source $logSource -EntryType Error -EventId $EvtIDError -Message "Something went wrong. Please try again later!"
    }
    # removing the downloaded web drivers zip file
    Write-EventLog -LogName $logName -Source $logSource -EntryType Information -EventId $EvtIDOK -Message "Removing the web drivers zip file..."
    Remove-Item $webDriverZip -Force
}

I know it is not a good-looking script but it gets the job done. The script checks and compares the web browser and web driver versions – if they don’t match – it pulls the right web driver version, extracts it, replaces the old version, and everything is recorded in Windows event logs. If versions match, it does nothing but write a log entry. The following image shows how the event log entry will look like:

WebDriver_Update_Log

How to Run the Script

You just need to copy the code and save it to a file with .ps1 extension. To save time, you could download my script. To run the downloaded script, you might need to change the execution policy for current PowerShell process as shown in this article. To learn more about writing and running PowerShell scripts, you can read this article.

Once you’re able to run this PowerShell script manually, you could create a scheduled task to keep the web browser and web driver version in sync. Every time your web browser is updated (either automatically or manually), the relevant web driver will be automatically updated when the scheduled script is executed. Read this article to learn how to trigger a scheduled task when a program is launched.



Microsoft Certified | Cisco Certified

Leave a Reply