Web automation is a powerful tool, especially in the hands of IT professionals who know how to use it. It’s also one that many people don’t fully understand, so this article will walk you through what web automation is and what tools you can use with PowerShell today.
“Powershell selenium chrome options” is a guide that will teach you how to get started in web automation with PowerShell and Selenium. This article was written by one of the leading experts in web automation, Brian Keller.
It’s not enjoyable to have to type and click things over and over again to complete a task. Take, for example, website testing and monitoring. You do several repeated tasks, such as clicking on buttons and links, logging in, conducting a search, and clicking again. Selenium and PowerShell come in handy.
Selenium is a framework for automating web browser testing that is free and open-source. Selenium is also compatible with PowerShell since it is portable and supports various languages.
In this post, you’ll discover how to get started automating web-related activities on web browsers utilizing the fantastic combo of these two outstanding technologies, Selenium and PowerShell. You’ll learn how to do tasks like browsing, logging, searching, clicking, and submitting input programmatically.
You’ll have written a PowerShell script that accomplishes the following by the conclusion of this article:
- Open a new tab with Google Chrome.
- Go to PowerShell.org to learn more about PowerShell.
- Please enter your username and password. (Login)
- Perform a search for articles.
- In the results, choose the second article.
If you’re still interested, prepare to learn by continuing to read.
Prerequisites
Before continuing, you must satisfy a few prerequisites in order to follow along with the examples.
- You’ll need a PC that runs Windows 10 or above.
- Windows PowerShell 5.1 or PowerShell 7.0.3 must be installed on your PC.
- A code editor must be installed on your PC. You’ll see that Visual Studio Code is utilized in this article. Use whichever programming editor you’re most familiar with.
- The Google Chrome browser must be installed on your machine.
- ChromeDriver version that corresponds to your Google Chrome version.
Chrome Plug-in
- Selenium Web Driver in its most recent stable version. The most recent version is 3.14.0 as of this writing. Download the web driver for C# from the downloads page.
The Selenium Web Driver for C# is available for download.
It’s important to note that after you’ve downloaded the Selenium WebDriver zip file, you’ll need to hunt for the WebDriver.dll file explicitly.
Locate the WebDriver.dll file in the Selenium zip package.
- Make a directory for your script, such as c:selenium, to work in.
- Ensure that chromedriver.exe and WebDriver.dll are extracted to your working directory.
- In your working directory, create a new PowerShell script file called run.ps1. The files you should have in your working directory are shown in the figure below.
Working directory for Selenium
How to Use Selenium PowerShell
To make Selenium PowerShell function, you’ll need two things.
- In order to use WebDriver.dll, you must first import it into your PowerShell session.
- ChromeDriver.exe may be run from either the system or user paths.
You’ll learn how to ensure sure certain requirements are satisfied in this section.
In the User Path, add the ChromeDriver Location
For ChromeDriver to operate, the path of ChromeDriver.exe must be in the Path environment variable, according to the official instructions. The Selenium WebDriver can detect the location using either the system or user Path environment variables.
The command below will add the folder c:selenium to the user path of the current PowerShell session.
# The location of your working directory ‘C:selenium’ as $workingPath # The working directory should be added to the environment path. # This is essential for ChromeDriver to function properly. $env:Path += “;$workingPath” if (($env:Path -split ‘;’) -notcontains $workingPath)
After performing the above command in your PowerShell session, execute the command below to verify that the c:selenium location was added to the environment path.
The c:selenium location is now visible in the environment route, as seen in the figure below.
In the user path, there is a spot for ChromeDrive.
Using PowerShell to import Selenium
You may then use any of the instructions below to import WebDriver.dll (class) into the current PowerShell session. It’s important to note that you only have to choose one. The first command should suffice, but feel free to use whatever one you choose. The end outcome will be same.
# OPTION 1: Use the Add-Type cmdlet to import Selenium into PowerShell. Add-Type -Path “$($workingPath)WebDriver.dll” Add-Type -Path “$($workingPath)WebDriver.dll” # OPTION 2: Use the Import-Module cmdlet to import Selenium into PowerShell. Import-Module “$($workingPath)WebDriver.dll” Import-Module “$($workingPath)WebDriver.dll” # OPTION 3: Use the.NET assembly class to import Selenium into PowerShell. [System.Reflection.Assembly]::LoadFrom(“$($workingPath)WebDriver.dll”)
Creating a Browser Instance from Scratch
You may now build a new instance of the ChromeDriver in your PowerShell session once you’ve imported the Selenium WebDriver. Then, using that instance, open a browser and go to a URL.
If the form of the $ChromeDriver variable is true, the command below creates a new ChromeDriver object.
# Create a new instance of the ChromeDriver Object. $ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver; $ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver; $ChromeDriver
The following command will open a browser window and go to the URL supplied.
# Launch a browser and go to URL $ChromeDriver.Navigate().GoToURL(‘<https://powershell.org/profile/login/>’)
A new browser window opens once you execute the code above in your PowerShell session, and the URL is automatically loaded.
“Chrome is being controlled by automated test software,” states a notice text below the address bar in the image below, indicating that this browser instance was launched by the web driver.
Using Selenium PowerShell commands, a web browser is started.
Examining the Elements on a Page
The term “element” refers to everything on the loaded web page. Buttons, input boxes, and links are examples of these components. You must first find any element before you can interact with it programmatically.
A built-in developer tool is included in modern browsers, such as Chrome. This developer tool may be used to check the page’s components and get the attributes you want.
For example, the web page shows a login form with a username, password, and login button. To inspect these elements, press CTRL+SHIFT+I on your keyboard to display the dev tool pane on the right. Alternatively, click on the menu button on the top right, click More tools —> Developer Tools.
In the browser, open the Developer Tools.
Right-click on the element you want to investigate once the developer tool window appears. In this case, right-click on the username input field and choose Inspect from the menu.
Examine a component
Then, right-click on the highlighted element code and click on Copy —> Copy XPath.
Copy an element’s XPath.
Make careful to save the XPath value in a record now that you’ve copied it. Then repeat the procedure, copying the XPath of the Password box and Login button in the process. You should keep a record of the three elements’ XPaths, similar to the one shown below.
# XPath for the Username Box: /*[@id=”username or email-731″] # XPath for the Password Box: /*[@id=”user pass-731″] # XPath for Login Button: /*[@id=”post-115080″] /div/div/div/div[2]/form/div[7]/input
Using XPath to find the target elements, sending keys, and clicking
Use the code below to issue each element’s instructions now that you know the XPath of the Username box, Password box, and Login button. Make sure the XPath values within the FindElementByXPath function are changed (). Also, replace your username and password in the SendKeys() values of the first two code lines.
# Use $ChromeDriver.FindElementByXPath(‘/*[@id=”username or email-731″]’ to get the username in the Username field. SendKeys(‘HERE IS YOUR USERNAME’) # Use $ChromeDriver.FindElementByXPath(‘/*[@id=”user pass-731″]’ to find the password in the Password field. SendKeys(‘HERE IS YOUR PASSWORD’) # Click the Login button $ChromeDriver.FindElementByXPath(‘/*[@id=”post-115080″]) $ChromeDriver.FindElementByXPath(‘/*[@id=”post-115080″]) $ChromeDriver.FindElementByX /div/div/div/div[2]/form/div[7]/input’). Click()
Run the code one line at a time in PowerShell after substituting your XPath, username, and password values. The code programmatically input the username and password and clicked the Login button, as seen in the sample below.
Using Selenium PowerShell, log into a webpage.
Then, on the left side, obtain the XPath of the SEARCH menu.
Get the SEARCH menu item’s XPath.
Substitute the FindElementByXPath() value in the code below after retrieving the XPath. Then, in PowerShell, execute the code.
# Select $ChromeDriver from the SEARCH menu. FindElementByXPath(‘/*[@id=”menu-item-194315″]/a/span’). Click()
The Selenium PowerShell command is used to click the SEARCH menu link in the example below.
Using Selenium PowerShell to click a menu
Providing Data for Input
Now that you’ve arrived at the Search page, you’ll need to input a keyword search to do an article search. However, you must first get the XPath of the Search… box.
Get the Search box’s XPath.
When you have the XPath of the search box, type the term “SharePoint” in the box and submit the search using the code below. Make sure the FindElementByXPath() value is replaced.
# To search $ChromeDriver, type in a term. FindElementByXPath(‘/*[@id=”s”]’). SendKeys(‘Sharepoint’) # Use $ChromeDriver.FindElementByXPath(‘/*[@id=”s”]’) to submit the search. Submit()
When you run the instructions above in PowerShell, the search should be completed, much as in the example below.
Using Selenium PowerShell to submit a search query
Using Tags to Find the Target Elements and Sending Clicks
The search was submitted and the results were returned in the previous section. After that, you’ll need to execute a code to click the second search result. Using XPath is no longer appropriate since you are dynamically picking an element depending on the order in which the search results are shown.
Using the TagName of an element is one of the strategies that may be used to find it. You can see that the tag name is article in the example below. There will be many elements with the article tag name since there are multiple search results.
Examining the pieces and obtaining tag names
The url is what you should be searching for inside the article. The tagname an is used for hyperlinks. Underneath an article, there may be more than one hyperlink or tagname. The article’s hyperlink is the first tagname in the example above.
You’ll need to utilize an index to figure out which links to click if you want to access the second search result by clicking the first hyperlink in that search result.
The following code does the following:
- Use the tag name ‘article’ to find the second element. [1] = Index
- Use the tag name ‘a’ to find the first element under the ‘article’. [0] as the index
- To see the article, click on the link.
It’s worth noting that the index is zero-based. This implies that [0] is the first index, [1] is the second, and so on.
# Click the link $ChromeDriver in the second search result. FindElementsByTagName(‘article’)[1]. FindElementsByTagName(‘a’)[0]. Click()
The desired outcome is seen below when the code is executed in PowerShell.
Using Selenium PowerShell to find items by tagname
Organizing
It’s critical to clean up Selenium when your automation has completed its task, which includes clearing temporary files and closing browser processes. The cleanup does not have to be done manually. All you have to do is run the lines shown below. This step is optional, although it is encouraged.
$ChromeDriver.Close() $ChromeDriver.Quit()
Putting Your Code Together
The resultant code, which you may store in your run, is based on all of the stages and tests you’ve completed. Script for PS1.
# The location of your working directory ‘C:selenium’ as $workingPath # The working directory should be added to the environment path. # This is essential for ChromeDriver to function properly. $env:Path += “;$workingPath” if (($env:Path -split ‘;’) -notcontains $workingPath) # OPTION 1: Import Selenium to PowerShell using the Add-Type cmdlet. Add-Type -Path “$($workingPath)WebDriver.dll” # Create a new instance of the ChromeDriver Object. $ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver; $ChromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver; $ChromeDriver # Launch a browser and go to URL $ChromeDriver.Navigate().GoToURL(‘<https://powershell.org/profile/login/>’) # Use $ChromeDriver.FindElementByXPath(‘/*[@id=”username or email-731″]’ to get the username in the Username field. SendKeys(‘HERE IS YOUR USERNAME’) # Use $ChromeDriver.FindElementByXPath(‘/*[@id=”user pass-731″]’ to find the password in the Password field. SendKeys(‘HERE IS YOUR PASSWORD’) # Click the Login button $ChromeDriver.FindElementByXPath(‘/*[@id=”post-115080″]) $ChromeDriver.FindElementByXPath(‘/*[@id=”post-115080″]) $ChromeDriver.FindElementByX /div/div/div/div[2]/form/div[7]/input’). Click() # Select $ChromeDriver from the SEARCH menu. FindElementByXPath(‘/*[@id=”menu-item-194315″]/a/span’). Click() # To search $ChromeDriver, type in a term. FindElementByXPath(‘/*[@id=”s”]’). SendKeys(‘Sharepoint’) # Use $ChromeDriver.FindElementByXPath(‘/*[@id=”s”]’) to submit the search. Submit() # Click the link $ChromeDriver in the second search result. FindElementsByTagName(‘article’)[1]. FindElementsByTagName(‘a’)[0]. Click() # Cleanup $ChromeDriver.Close() $ChromeDriver.Quit()
Summary
For browser-based automation, Selenium and PowerShell are a potent combo. You can automate processes that can be repeated and develop scripts that can be reused using Selenium and PowerShell.
You learnt how to get started with Selenium and PowerShell for browser-based web automation in this post. You’ve learned how to analyze items on a page, identify elements, and programmatically send actions to the web browser to accomplish certain tasks.
I hope this post has given you a better knowledge of Selenium and what it can do when used in conjunction with PowerShell to construct automation scripts. Making a script to download or upload files to a website may be an useful follow-up learning exercise.
Additional Reading
The “find element by xpath selenium powershell” is a tutorial on how to automate web pages with PowerShell and Selenium.
Frequently Asked Questions
Can PowerShell be used for automation?
A: PowerShell is a scripting and automation language that can be used for many tasks, but it cannot be called automated as of now.
Can PowerShell interact with webpages?
A: Yes, PowerShell can interact with webpages.
How do you automate a website using selenium?
A: A selenium program would be used to interact with the website and create an algorithm that auto-generates results. The browser provides a user interface for users to see these generated results in real time, which is what makes it automated.
Related Tags
- selenium powershell documentation
- powershell chrome automation
- powershell selenium wait for page to load
- powershell web automation
- powershell script to login to website automatically