PowerShell & Selenium: Automate Web Browser Interactions – Part III

Here we are! Part 3. Last one unless someone decides to make a sequel in 3 years. Let's cut to the chase and go straight into logon page automation.

In case you want to go over the basics of what's going on here, you may want to check out the previous 2 parts:

Part I
Part II

On the previous posts we automated interactions with the google search page in our script, for the next example we will interact with Linkedin's login page. For this proof of concept the elements we need are the user (e-mail) input textbox and the password input textbox.

And here is the url: https://www.linkedin.com/uas/login

  1. Grab the username's textbox name ("session_key")

    Click to "embiggen"
  2. Grab the password's textbox name ("session_password")
Click to "embiggen"

Now let's go back to our script.

For starters we'll define the website, as mentioned earlier this time I will use Linkedin's login page and store it in a variable for later use.

$YourURL = "https://www.linkedin.com/uas/login"

Next, we need to gather the user and password for Linkedin, let's use the Get-Credential cmdlet to keep it simple, safe and sound rather than leaving our user and password as open text within the script. Of course you may adapt this to your needs, there's more than one way to store passwords within your script. For now I chose something in-between ease and security.

$PSCred = Get-Credential -Message 'Enter Username and Password'

Now, Selenium get's called into our script with the following, same as in the previous examples:

$env:PATH += ";C:\Temp\PSL\" # Adds the path for ChromeDriver.exe to the environmental variable 
Add-Type -Path "C:\Temp\PSL\WebDriver.dll" # Adding Selenium's .NET assembly (dll) to access it's classes in this PowerShell session
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver # Creates an instance of this class to control Selenium and stores it in an easy to handle variable

Don't forget to save this script as a .ps1 file under "C:\Temp\PSL\", I will keep the same name as with the other versions: "psl.ps1" .

Now, we'll use Selenium's methods to navigate to the webpage, send the user/password combo and submit the information. The username and password properties are going to be stored in the $PSCred variable.

$ChromeDriver.Navigate().GoToURL($YourURL) # Browse to the specified website
$ChromeDriver.FindElementByName("session_key").SendKeys($PSCred.Username) # Methods to find the input textbox for the username and type it into the textbox
$ChromeDriver.FindElementByName("session_password").SendKeys($PSCred.GetNetworkCredential().password) # Methods to find the input textbox for the password and type it into the textbox
$ChromeDriver.FindElementByName("session_password").Submit() # We are submitting this info to linkedin for login # From the same textbox, submit this information to Linkedin for logging in
Click to "embiggen"

So what we are doing here is

1: Find the input textbox "session_key" and send the username stored
2: Find the input textbox "session_password" and send the password stored
3: From the "session_password" textbox, submit this information to log in

To complete this script, I added a few extra lines at the bottom just to remove the chromedriver instances and also to remove the stored user/password for a little wee layer of extra security, our code so far should look like this:

# Website and credential variables
$YourURL = "https://www.linkedin.com/uas/login" # Website we'll log to
$PSCred = Get-Credential -Message 'Enter Username and Password' # Get credentials for login page

# Invoke Selenium into our script!
$env:PATH += ";C:\Temp\PSL\" # Adds the path for ChromeDriver.exe to the environmental variable 
Add-Type -Path "C:\Temp\PSL\WebDriver.dll" # Adding Selenium's .NET assembly (dll) to access it's classes in this PowerShell session
$ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver # Creates an instance of this class to control Selenium and stores it in an easy to handle variable

# Make use of Selenium's class methods to manage our browser at will
$ChromeDriver.Navigate().GoToURL($YourURL) # Browse to the specified website
$ChromeDriver.FindElementByName("session_key").SendKeys($PSCred.Username) # Methods to find the input textbox for the username and type it into the textbox
$ChromeDriver.FindElementByName("session_password").SendKeys($PSCred.GetNetworkCredential().password) # Methods to find the input textbox for the password and type it into the textbox
$ChromeDriver.FindElementByName("session_password").Submit() # We are submitting this info to linkedin for login # From the same textbox, submit this information to Linkedin for logging in

# Cleaning up after ourselves!
Pause # Adding a stop, after pressing enter within the console the Selenium session will end everything will be closed
Remove-Variable -Name PSCred # This removes the stored user/password from within the $PSCred variable
Function Stop-ChromeDriver {Get-Process -Name chromedriver -ErrorAction SilentlyContinue | Stop-Process -ErrorAction SilentlyContinue}
$ChromeDriver.Close() # Close selenium browser session method
$ChromeDriver.Quit() # End ChromeDriver process method
Stop-ChromeDriver # Function to make sure Chromedriver process is ended (double-tap!)

And in practice, here is what it does:

Click to "embiggen"

That's all folks! With this we  conclude the short PowerShell + Selenium series, I hope this inspires you or helps you build something to help you out in the future, it's a nifty tool to have in your arsenal!

For now, thank you and have fun with your automation adventures!
PowerShell & Selenium: Automate Web Browser Interactions – Part I
PowerShell & Selenium: Automate Web Browser Interactions – Part II

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.