HomeAbout

Using Selenium for Web Automation with PowerShell

By Denis Molodtsov
Published in PowerShell
May 08, 2023
1 min read
Using Selenium for Web Automation with PowerShell

Table Of Contents

01
Why use Selenium with PowerShell?
02
Configure Selenium with PowerShell
03
Open a page, enter login and password.
04
Click the login button
05
Inject JavaScript code to the page
06
Closes the browser

Why use Selenium with PowerShell?

When it comes to automating browser-based tasks, two of the most popular tools in the market are Selenium and Power Automate RPA. While both of them have their own advantages, the choice between the two primarily depends on the specific use case.

Selenium is an open-source tool used for automating web applications for testing purposes, but it is also used for repetitive web-based administration tasks. Power Automate RPA, on the other hand, is a solution by Microsoft that allows end-to-end automation of tasks, involving multiple applications and data sources. While Power Automate RPA’s capabilities are vast, its power could be an overkill for certain situations.

The reasons to choose Selenium over Power Automate RPA are:

Cost-Effectiveness: One of the most compelling reasons to choose Selenium is that it is entirely free. On the other hand, Power Automate RPA comes with a cost that can become significant, especially for large-scale projects or for organizations with tighter budgets.

Simplicity: If your project involves simple, web-based tasks, then Selenium could be a more straightforward choice. Power Automate RPA excels in scenarios where there’s a need to integrate with multiple systems, data sources, or complex business rules. However, for basic operations such as navigating web pages, filling forms, or testing web applications, Selenium can be more than sufficient.

Customization and Flexibility: Selenium offers the ability to write scripts in various programming languages including Java, C#, Python, Ruby, etc., providing an excellent level of flexibility and customization based on the user’s programming skill and preference.

Community Support: Being open-source and widely used around the globe, Selenium has an extensive user community. This means you can easily find solutions and guidance online from developers who have faced similar issues.

Configure Selenium with PowerShell

First, let’s install Selenium PowerShell module.

  • Run Install-Module Selenium in Windows PowerShell.

Selenium supports multiple browsers, such as Chrome, Firefox, Edge, and Safari. Download the driver for the browser you want to use. For example, here is a Chrome driver: ChromeDriver: https://chromedriver.chromium.org/downloads At the time of writing, the current driver version was 114 (same as the Chrome or Edge version).

  • Place the chromedriver.exe file inside the Selenium PowerShell module. For example: C:\WindowsPowerShell\Modules\Selenium\3.0.1\assemblies. To get the exact Selenium module location, you can run this PowerShell script:
Import-Module Selenium
$Module = get-module Selenium
$Module.Path

Open a page, enter login and password.

# Install-Module -Name Selenium # -scope CurrentUser
# Import Selenium module
Import-Module Selenium
# Start Chrome browser
$Driver = Start-SeChrome
# Navigate to a specific URL
$Driver.Navigate().GoToUrl('https://Contoso.com/Document/import/Messages.aspx')
# Find the button element by ID
$EmailAndPasswordRadio = $Driver.FindElementById('ctl00_ctl00_PageContentPlaceHolder_PageContentPlaceHolder_LoginProviderRadioButtonList_1')
$EmailAndPasswordRadio.Click()
$SetLoginMethodButton = $Driver.FindElementById('ctl00_ctl00_PageContentPlaceHolder_PageContentPlaceHolder_SaveLoginProviderButton')
$SetLoginMethodButton.Click()
# Enter login
$LoginElement = $Driver.FindElementById('ctl00_ctl00_PageContentPlaceHolder_PageContentPlaceHolder_EmailAddressTextBox')
$LoginElement.SendKeys('contoso@microsoft.com')
# Enter password
$PasswordElement = $Driver.FindElementById('ctl00_ctl00_PageContentPlaceHolder_PageContentPlaceHolder_PasswordTextBox')
$PasswordElement.SendKeys('SamplePasswordFake')

Click the login button

# Click the login button
$LoginButton = $Driver.FindElementById('ctl00_ctl00_PageContentPlaceHolder_PageContentPlaceHolder_LoginButton')
$LoginButton.Click()

Inject JavaScript code to the page

# Read the custom JavaScript file to the page
$JavaScriptCode = Get-Content -Path "..\JavaScript\Click-the-form.js" -Raw
# Inject and execute the JavaScript code in the context of the current web page
$Driver.ExecuteScript($JavaScriptCode)

Closes the browser

# Close the browser window
$Driver.Quit()

Tags

SeleniumAutomationPowerShell

Share

Previous Article
Apply Modern Theme in SharePoint Subscription Edition
Denis Molodtsov

Denis Molodtsov

Microsoft 365 Architect

Related Posts

Run PnP PowerShell from Azure Automation
Run PnP PowerShell from Azure Automation
June 13, 2024
3 min

Quick Links

About

Social Media