Created
November 10, 2023 07:59
-
-
Save HotCakeX/42512e2f208031b29cb03fe5a1dc8c98 to your computer and use it in GitHub Desktop.
How to Install Edge WebDriver and Use Selenium With It
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[System.String]$RegistryPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\msedge.exe' | |
[System.String]$SeleniumDirectory = 'C:\EdgeSelenium' | |
# Check to see if Microsoft Edge is installed | |
if (-not (Test-Path -Path $RegistryPath)) { | |
# Write an error if Edge was not found | |
Write-Error -Message 'Microsoft Edge needs to be installed!' | |
# Exit the script and set a return code | |
exit 1 | |
} | |
# Get the currently installed version of the Microsoft Edge | |
[System.String]$CurrentEdgeVersion = (Get-Item (Get-ItemProperty $RegistryPath).'(Default)').VersionInfo.ProductVersion | |
# Check the CPU architecture to see if it is supported | |
switch ($env:PROCESSOR_ARCHITECTURE) { | |
'AMD64' { [System.String]$EdgeDriverDownloadLink = "https://msedgedriver.azureedge.net/$CurrentEdgeVersion/edgedriver_win64.zip" } | |
'ARM64' { [System.String]$EdgeDriverDownloadLink = "https://msedgedriver.azureedge.net/$CurrentEdgeVersion/edgedriver_arm64.zip" } | |
'x86' { [System.String]$EdgeDriverDownloadLink = "https://msedgedriver.azureedge.net/$CurrentEdgeVersion/edgedriver_win32.zip" } | |
Default { | |
Write-Error -Message 'CPU not supported' | |
exit 2 | |
} | |
} | |
# Verify the existence of Edge web driver and Nuget.exe, also make sure the Edge driver is up to date | |
if ( | |
(Test-Path -Path "$SeleniumDirectory\msedgedriver.exe") -and | |
(Test-Path -Path "$SeleniumDirectory\NuGet.exe") -and | |
((Get-Item -Path "$SeleniumDirectory\msedgedriver.exe").VersionInfo.ProductVersion -eq $CurrentEdgeVersion) | |
) { | |
# Execute commands relative to the Edge folder and skip download and installation steps | |
Set-Location -Path $SeleniumDirectory | |
} | |
else { | |
# Grab the edge web driver executable | |
New-Item -Path $SeleniumDirectory -ItemType 'Directory' -Force | Out-Null | |
# Execute commands relative to the Edge folder that was just created | |
Set-Location -Path $SeleniumDirectory | |
# Download the correct version of the web driver based upon the currently installed version of EdgeDev | |
# Download the latest version of NuGet | |
# Create an array of files to download | |
[System.Object[]]$Files = @( | |
# System.Net.WebClient requires absolute path instead of relative one | |
@{url = "$EdgeDriverDownloadLink"; path = "$SeleniumDirectory\WebDriver.zip" } | |
@{url = 'https://dist.nuget.org/win-x86-commandline/latest/nuget.exe'; path = "$SeleniumDirectory\NuGet.exe" } | |
) | |
# Start a job for each file download | |
[System.Object[]]$Jobs = foreach ($File in $Files) { | |
Start-ThreadJob -ErrorAction Stop -ScriptBlock { | |
param($Url, $Path) | |
# Create a WebClient object | |
[System.Net.WebClient]$WC = New-Object System.Net.WebClient | |
# Download the file | |
$WC.DownloadFile($Url, $Path) | |
} -ArgumentList $File.url, $File.path | |
} | |
# Wait until all jobs are completed | |
while ($Jobs | Where-Object { $_.State -ne 'Completed' }) { | |
Start-Sleep -Milliseconds 700 | |
} | |
# Receive the output or errors of each job and remove the job | |
foreach ($Job in $Jobs) { | |
Receive-Job -Job $Job -ErrorAction Stop | |
Remove-Job -Job $Job -ErrorAction Stop | |
} | |
# Extract the Edge web driver | |
Expand-Archive -Path .\WebDriver.zip -DestinationPath .\ -Force | |
# Clean up after extraction | |
Remove-Item -Path '.\WebDriver.zip' -Force | |
# Install selenium lib | |
.\NuGet.exe --% install Selenium.WebDriver | |
} | |
# import the selenium lib | |
Add-Type -Path (Get-ChildItem -Path '.\Selenium.WebDriver.*\lib\netstandard*\WebDriver.dll' -File).FullName | |
# Create an EdgeOptions object (optional) | |
# $EdgeOptions = New-Object OpenQA.Selenium.Edge.EdgeOptions | |
# Set the headless mode (optional) | |
# $EdgeOptions.AddArgument("--headless") | |
# instantiate with optional options | |
# [OpenQA.Selenium.Chromium.ChromiumDriver]$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver('C:\EdgeSelenium\msedgedriver.exe', $EdgeOptions) | |
# instantiate without options | |
[OpenQA.Selenium.Chromium.ChromiumDriver]$EdgeDriver = New-Object OpenQA.Selenium.Edge.EdgeDriver('C:\EdgeSelenium\msedgedriver.exe') | |
# Navigate to a website | |
$EdgeDriver.Navigate().GoToUrl('https://www.bing.com') | |
# Perform other actions with the driver | |
# Quit the driver | |
# $EdgeDriver.Quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to Install Edge WebDriver and Use Selenium With It
Made a PowerShell script to automate downloading, installation and running Edge Web Driver with Selenium