Using Selenium WebDriver with PowerShell



Hola todos... que pasa?

This time I am going to show an example on how to use Selenium WebDriver with PowerShell to open the browser and do stuff like checking a checkbox or maybe writing some text in a text box.

This one took me sometime to deal with it, it was not a straightforward thing, I spent so much time in front of my laptop, I lost the feeling on my ass for some time after finishing the code, anyway, To find more information about Selenium and how it works, you can dig this link : http://www.seleniumhq.org/

this WebDriver was built for C#, and of course it can be used and called withing PowerShell or Vb.net easily...


Anyway, let’s talk about the code :


#Code Start Here :

# moving to the folder where Selenium DLL files are…
# You can also use Add-Type Cmdlet with -path option, it will do the same.

cd "d:\NonOfficialPSModules\selenium-dotnet-2.46.0\net40"
[System.Reflection.Assembly]::LoadFrom("d:\NonOfficialPSModules\selenium-dotnet-2.46.0\net40\Selenium.WebDriverBackedSelenium.dll")
[System.Reflection.Assembly]::LoadFrom("d:\NonOfficialPSModules\selenium-dotnet-2.46.0\net40\ThoughtWorks.Selenium.Core.dll")
[System.Reflection.Assembly]::LoadFrom("d:\NonOfficialPSModules\selenium-dotnet-2.46.0\net40\WebDriver.dll")
[System.Reflection.Assembly]::LoadFrom("d:\NonOfficialPSModules\selenium-dotnet-2.46.0\net40\WebDriver.Support.dll")


# calling the Browser Driver, in this case I am using FireFox
$dr = New-Object "OpenQA.Selenium.FireFox.FirefoxDriver"

$actions = New-Object  OpenQA.Selenium.Interactions.Actions($dr)
# Using Facebook as a test

$dr.url = "https://www.facebook.com/"

# Pausing for sometime to allow the browser to load all the elements inside
# The web Page.
Start-Sleep -Seconds 20

# I am using .NET's Windows Forms here to send two TABs (TAB clicks)
# because when the browser start, the cursor will point to the address bar,
# To move the cursor inside the browser itself I am pressing TAB two times to 
# Make sure that the cursor is moved on one of the web elements this will make
# sure that the cursor will move to the correct element later.
[System.Windows.Forms.SendKeys]::SendWait("{TAB}") | out-null
[System.Windows.Forms.SendKeys]::SendWait("{TAB}") | out-null

#Looking for the element, in my case, it's the checkbox
# the one near the label : keep me logged in

$MyElement = $dr.FindElementByXpath("//*[@class='uiInputLabelInput uiInputLabelCheckbox']")

# This "if" statement will check if the above variable is true (Means the object
# was found)
if ($MyElement){
Write-Host "found it.."
# This is just some information, you can comment this if you want
$actions.GetHashCode()
$actions.GetType()

# This is the main thing here, it will take the focus from any element in the
# page and move it to the checkbox than simply click the check box to check it.
$actions.MoveToElement($MyElement).Click().Perform()
# sleeping for sometime will allow you to see the changes 
Start-Sleep -Seconds 10
#Close the browser and finish the code.
$dr.Close()
}

# in case the element was not found it will show a message and simply close the
# browser
if (! $MyElement){
Write-Host "Element Not found..Check your Reff Again.."
#Close the browser and finish the code.
Start-Sleep -Seconds 10
$dr.Close()

}
#Code End Here.





You can look for elements using the developer tools in browsers, you can find elements by different ways include name, Id and other properties. I explained Every part of the code as comments, hope everyone understand it.

you can use the same code above if you want to write some text in a textbox element.

to send keyboard signals to the element you can use this statement :

[System.Windows.Forms.SendKeys]::SendWait("TextHere") | out-null


It's the same way I used to send the TAB signals above...


sadly I gotta go now, I have to cook some food, see ya all later..Hasta Pronto

Comments

Popular posts from this blog

IP Calculator in PowerShell...with IP exclusion