In a previous post, PowerShell: Install-Module and Uninstall-Module, it was shown how to install the Selenium module for PowerShell and with respect to installation using the all users scope. The Selenium module for PowerShell is an open source project hosted on Git Hub: https://github.com/adamdriscoll/selenium-powershell.
The https://github.com/adamdriscoll/selenium-powershell Git Hub home page includes a Readme.md page which contains a usage section describing how to use the PowerShell module to automate browsers with Selenium:
The URL used to demonstrate browser automation is https://www.google.com. A previous blog post showed how to inspect the element on this page in which the search term is entered, Chrome: Fundamentals of Browser Automation.
The PowerShell code to enter a search term and invoke search via the Enter key is straightforward. The term searched for will be "The Wedding Present Dalliance" which of course refers to the song "Daliance" but the band, The Wedding Present.
The browser automation code is as follows:
$driver = $null
try {
$driver = Start-SeChrome
Enter-SeUrl 'https://www.google.com' -Driver $driver
# The name of the input element is q (name="q")
# <input class="gLFyf gsfi" maxlength="2048" name="q"
$element = Find-SeElement -Driver $driver -Name 'q'
Send-SeKeys -Element $element `
-Keys "the wedding present dalliance`n"
}
catch {
Write-Host $PSItem.Exception
}
finally {
if ($null -ne $driver) {
Stop-SeDriver $driver
$driver = $null
}
}
Chrome is launched and terminated using the Start-SeChrome and Stop-SeDriver functions which utilize PowerShells try/finally construct to insure that the Chrome is closed even if the script generates an error:
$driver = $null
try {
try {
$driver = Start-SeChrome
# ... additional code here ....
}
finally {
if ($null -ne $driver) {
Stop-SeDriver $driver
$driver = $null
}
}
if ($null -ne $driver) {
Stop-SeDriver $driver
$driver = $null
}
}
Each browser supported by Selenium has it own start function:
Start-SeChrome: starts Chrome- Start-SeEdge: starts the version of Edge that is not implemented with Chromium
- Start-SeNewEdge: starts the version of Edge that is implemented with Chromium
- Start-SeFirefoxL starts Firefox
- Start-SeInternetExplorer: starts Internet Explorer
The URL is assigned using the function, Enter-SeUrl:
Enter-SeUrl 'https://www.google.com' -Driver $driver
The input element is selected by name using the fuction, Find-SeElement:
# The name of the input element is q (name="q")
# <input class="gLFyf gsfi" maxlength="2048" name="q"
# <input class="gLFyf gsfi" maxlength="2048" name="q"
$element = Find-SeElement -Driver $driver -Name 'q'
The text to search on and the Enter key to invoke search is invoked using function, Send-SeKeys:
Send-SeKeys -Element $element `
-Keys "the wedding present dalliance`n"
The result of running the script is the as anticipated search results:
No comments :
Post a Comment