Jump to content

Using Selenium in PowerShell


JLogan3o13
 Share

Recommended Posts

  • Moderators

There are a number of posts on the forum regarding use of Selenium in AutoIt. I recently had a go at using the PowerShell Selenium module, and was amazed at how easy it is. Thought I would post an example here; if anyone is interested this could probably be incorporated into AutoIt code pretty easily. 

Pre-Req - The true star of this script is the ChroPath extension, available for Edge, Chrome and FireFox. With it installed, you just click on the element, select Inspect, and then ChroPath generates the XPath to the element for you. Here is an example based on a simple form I created on one of my sites.

$myForm = Start-SeChrome -StartURL "http://logancomputerser.com/Appointment.html" -Maximized
$firstName = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_First']"
$lastName = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Last']"
$address = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Street1']"
$city = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_City']"
$zip = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_Zip']"
$state = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//select[@id='formElement_State']"
$phoneDay = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_DaytimePhone']"
$phoneNight = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_EveningPhone']"
$email = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_liamE']"
$user = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_48564']"
$pw = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='formElement_f403c']"
$submit = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='wstForm_Contact_Submit']"
$reset = Find-SeElement -Driver $myForm -Timeout 30 -XPath "//input[@id='wstForm_Contact_Reset']"

Send-SeKeys -Element $firstName -Keys "Joe"
Send-SeKeys -Element $lastName -Keys "Blow"
Send-SeKeys -Element $address -Keys "111 S. Main St."
Send-SeKeys -Element $city -Keys "AnyCity"
Send-SeKeys -Element $zip -Keys "90210"
Send-SeKeys -Element $state -Keys "CA"
Send-SeKeys -Element $phoneDay -Keys "555.867.5309"
Send-SeKeys -Element $phoneNight -Keys "555.888.1212"
Send-SeKeys -Element $email -Keys "1Adam12@gmail.com"
Send-SeKeys -Element $user -Keys "JBlow"
Send-SeKeys -Element $pw -Keys "MyPassword"

Start-Sleep 1

Invoke-SeClick -Element $submit
Stop-SeDriver -Driver $myForm

 

As mentioned, this is just another way to skin the cat, but I found it a pretty fast way to initiate some easy testing in Selenium, and have used it a couple of times in projects now, both straight through PowerShell and wrapped in AutoIt.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

5 minutes ago, JLogan3o13 said:

The true star of this script is the ChroPath extension, available for Edge, Chrome and FireFox. With it installed, you just click on the element, select Inspect, and then ChroPath generates the XPath to the element for you

Nice tip

6 minutes ago, JLogan3o13 said:

As mentioned, this is just another way to skin the cat, but I found it a pretty fast way to initiate some easy testing in Selenium, and have used it a couple of times in projects now, both straight through PowerShell and wrapped in AutoIt.

What would be the advantages / disadvantages of this option over the Webdriver UDF?

Link to comment
Share on other sites

  • Moderators

@Danp2 as mentioned, just another way to skin the proverbial cat. In my case (and why I posted here instead of in the normal Examples thread) is using this in a larger script that cannot be done in AutoIt. 

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...