TECH
BLOG

Key points for using Selenium WebDriver with PowerShell

2020
4

beginning

I'll try using Selenium from PowerShell and summarize what I learned from installation to how to search for the features I want to use.

initiate

If you're a first-time user, I recommend this site.

I'm downloading the Selenium library directly from this sitenugetIf you use it, you can complete it with only a command, so it's easy.

Download the library

nuget install selenium.webdriver
nuget install Selenium.Support
nuget install selenium.webdriver.chromedriver

Initializing and shutting down Chrome

Please match the folder name version with the one you downloaded.

# Path settings
## Folders downloaded with nuget
$seleniumHome = '.'
## WebDriver.dll full path
$webdriverDllPath = convert-path (Join-Path $seleniumHome '\ selenium.webdriver.3.141.0\ lib\netstandard2.0\ webdriver.dll')
## WebDriver.support.dll full path
$webdriverSupportDllPath = Convert-Path (Join-Path $seleniumHome '\ Selenium.Support.3.141.0\ lib\netstandard2.0\ webdriver.support.dll')
## Path to the folder where chromedriver.exe is located
$chromeDriverDirPath = Convert-Path (Join-Path $seleniumHome '\ selenium.webdriver.chromedriver.80.0.3987.10600\ driver\ win32')
# DLL loading
Add-Type -Path $webdriverDllPath
Add-Type -Path $webdriverSupportDllPath
# Start Chrome
$chromeDriver = New-Object openQA.selenium.chrome.chromeDriver ($chromeDriverDirPath)
# Close Chrome
$chromedriver.quit ()

Once you understand the atmosphere

If you follow Selenium's official documentation, the functions and sample code for them are included, so it's helpful.

Features that seem to be particularly commonly used are also explained.

Selenium PowerShell Module

Once you know what you can do, you can use the PowerShell Module. This is all included, so you can use it without having to download individual dlls.

https://github.com/adamdriscoll/selenium-powershell

Installing the module

Install-Module -Scope currentUser Selenium

Samples are provided in the following folders.

https://github.com/adamdriscoll/selenium-powershell/tree/master/Examples

Starting and closing Chrome with this module is as follows.

# Importing Selenium Modules
try {
Import-Module -Name Selenium -ErrorAction Stop
}
catch {
Write-Host 'Importing the Selenium module failed. Please install it using the following command: Install-Module Selenium'
break
}
# Start Chrome
$Driver = Start-sechRome
if (! $Driver) {
Write-Host “The Selenium Driver was not running.” -ForegroundColor Yellow
return
}
# Close Chrome
Stop-sedriver -Driver $Driver

Learn more

When searching for namespaces and methods for classes you want to use, it's a good idea to search for them in the API reference.

RELATED PROJECT

No items found.