You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is an example script that will show you how to use the Selenium driver to preform a simple google search.
6
+
Using this example script you will learn the basic usage of the Selenium powershell module.
7
+
Each comment line will explain the line that will come after it so you can follow it in a step by step fashion.
8
+
#>
9
+
10
+
# The line below will Import-Module Selenium if it fails it will display the installation command and stop the script.
11
+
try{Import-Module-Name Selenium -ErrorAction Stop}catch{Write-Host'Importing the Selenium module failed. Please install it using the following command: Install-Module Selenium';break}
12
+
13
+
# Start the Selenium Chrome Driver
14
+
$Driver=Start-SeChrome
15
+
16
+
# Next we will check if the driver is running and if it's not running we will show a message. If the driver is running we will run the commands inside the if statment.
17
+
if($Driver){
18
+
# Now that we verified that the driver is running we can start doing cool things with it.
19
+
20
+
# Using the Enter-SeUrl command we can tell the driver to go to any URL we want in this case we'll go to https://google.com/ncr
21
+
# I used the /ncr in the end of the URL because I want google to always stay in english and not redirect to a specific language for consistency.
# After nevigating to the desired URL we can start interacting with elements on the page in this example we are going to find the search box and type something in it.
25
+
# We can find elements using different ways like the element id or the Name/ClassName/TagName and a few other ways.
26
+
# In the below code we're going to show you a few ways to solve this problem.
# By examining the HTML code of the google seach box we can see that the search box name is q so in the below example we'll use the name q to find its element
# The $AllInputElements contains all the input elements on the page if we want to find the specific element for the google search box we will need to loop through each input element in the $AllInputElements array and get the attibute we want in this case we're looking for the title attribute.
42
+
# And we only want to return the element that has a title equal to Search we can find this out based on the html code on the page.
# You can send special key strokes to the SearchBoxElement, you should use the Selenium Keys enum. For example, if we wanted to send an enter key stroke, you could do it like this
# When working with dynamic websites, it’s often necessary to wait awhile for elements to appear on the page. By default, Selenium won’t wait and you’ll receive $null from Find-SeElement because the element isn’t there yet. There are a couple ways to work around this.
55
+
# The first is to use the Wait-SeElementExists cmdlet to wait for the existence of an element in the document.
56
+
# The Wait-SeElementExists will also return the element once it is found so you can use in order to wait and then find elements on the page.
57
+
58
+
# This command will wait for the img elements for 10 seconds and then return it to you or time out if the element wasn't found on.
0 commit comments