How to use Selenium via Powershell

3 minute read

Intro

Nobody likes clicking on over and over the same thing just to get your job done. That is why I’ve started to automate things 😄. Everything goes smoothly if service provider gives you tools like APIs, Powershell cmdlets, WebHooks, etc. But how to get things done on website if those tools are no good? You can use Selenium! And what is Selenium? It is code driven testing framework which automates browser. With it help I were able to speed-up new project creation process in our company Jira which in this case (our Jira’s version is somewhere 6) I were not able to use REST API.

Let me reveal some of this magic ✨

Getting started

What you will need is Webdriver.dll you can grab some from here or download directly from NuGet and extract it with 7-zip. Secondly you will need browser driver. I’m using Chrome Driver but there are drivers for Firefox, Opera, Edge, Safari. You can get Chrome driver here. Lastly after you pick your driver be sure that you have installed browser itself 😅. It’s the best to put .dll and .exe in the same folder to which you will be reaching in code.
Time for some code.

Starting web driver

Firstly you need to load assembly to your workspace and executable to environmental path variable.

$PathToFolder = 'C:\Temp\Selenium'
[System.Reflection.Assembly]::LoadFrom("{0}\WebDriver.dll" -f $PathToFolder)
if ($env:Path -notcontains ";$PathToFolder" ) {
    $env:Path += ";$PathToFolder"
}

Done.
Now you can lunch Selenium driven browser from your Powershell console!

demo

Like that:

$ChromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions
$ChromeOptions.AddArgument('start-maximized')
$ChromeOptions.AcceptInsecureCertificates = $True

$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($ChromeOptions)
$ChromeDriver.Url = 'https://universecitiz3n.github.io'

You can see in this code that I’m creating two objects:

  • ChromeOptions -
    this is a class which helps you set your browser preferences. I’ve declared two options; Fullscreen is nice and second option is to save few lines of code when you bump into:

cert

Yeah be sure that you trust site even though its cert expired!!

  • ChromeDriver -
    this creates new instance of webdriver the moment you create object from this class your browser will come to life ⚡️.

Things you can do

Making you browser open specified site comes to this

$ChromeDriver.Url = 'https://yourwebsite.com'

You must remember to put link in https:// format.

How about clicking on stuff? Well you need to know ID of element or its position. Your best friend for that is Inspect option provided by browser.

inspect

One you have opened Inspect tab you will be able to identify web objects and with right Selenium command manipulate them! These are available methods within webdriver that you can use

#The ones I'm used to use
$ChromeDriver.FindElementById('')
$ChromeDriver.FindElementByXPath('')

#The rest
$ChromeDriver.FindElementByClassName('')
$ChromeDriver.FindElementByCssSelector('')
$ChromeDriver.FindElementByLinkText('')
$ChromeDriver.FindElementByName('')
$ChromeDriver.FindElementByPartialLinkText('')
$ChromeDriver.FindElementByTagName('')

If website is well designed you should be able to get IDs of objects and user FindElementById

IDs

In this case ID for Log in button is wp-submit and to click it just run code like:

$ChromeDriver.FindElementById('wp-submit').Click()

Now, what if some element has no ID? For me XPath works the best. Getting XPath value is simple as

XPath

and now you can easily click on anything! Just like that:

$ChromeDriver.FindElementByXPath('//*[@id="main"]/div[2]/div[1]/article/h2/a').Click()

awesome

Next big thing is to fill boxes with your text and again what you should start with is to fetch ID or XPath of textbox. If you got this you can pass you text to box simply with:

$ChromeDriver.FindElementsById('user_login').SendKeys('[email protected]')

login

Isn’t it great? 😁
I hope that I’ve opened new door of possibilities before you will be brave enough to fiddle with things that Selenium brings! Manual clicking with mouse is boring! 😎

Pro tip

Be sure after running scripts to properly clean your workspace. To do that paste at the end of your script those

$ChromeDriver.close()
$ChromeDriver.quit()

If you won’t do that browser will not correctly dump temporary files which are created after staring webdriver session.
You can find those files in %LocalAppData%\Temp

trash

See you in next! 😉 🧠