Working with deprecated Internet Explorer using PowerShell to automate it was very easy earlier. To automated clicks or auto type on Internet Explorer, I was just using native .net com objects, which was a very simple and comprehensive. But since Internet Explorer is deprecated, I switched to Google Chrome / Microsoft Edge browser to automate Web Sites actions such as typing on textbox, clicking button etc, etc..
Below is the sample code for Internet Explorer browser automation.
$ie = New-Object -ComObject internetexplorer.application $ie.Navigate('http://webServer/index.html') $ie.Visible = $true Start-Sleep -Seconds 15 $ie.document.getElementById("button").click()
As for Microsoft Edge or Google Chrome browsers, there is no native or has inbuilt .net objects, we will need to use Selenium to automate those browsers. First you will need to download prerequisite Selenium WebDriver.dll, WebDriver.Support.dll and chromedriver.exe (for chrome browser) files.
Selenium is a collection of open-source tools that can automate web application testing and administration tasks.
Below are URLs from where you can download latest versions of the .dll and .exe files. Once downloaded latest .nupkg, rename it to .zip extension and extract .dll and .xml files.
https://www.nuget.org/packages/Selenium.Support/
https://www.nuget.org/packages/Selenium.WebDriver
Note: Copy all supporting .xml files as well with .dll
https://chromedriver.chromium.org/downloads
https://googlechromelabs.github.io/chrome-for-testing/#stable
https://www.nuget.org/packages/Selenium.WebDriver.ChromeDriver/
https://chromedriver.storage.googleapis.com/LATEST_RELEASE
Note: Every time Google releases and you update chrome browser version, You will need to download latest version of supporting .dll and .exe files.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# Load the necessary assemblies #[System.Reflection.Assembly]::LoadFrom(".\WebDriver.dll") Add-Type -Path "$PSScriptRoot\WebDriver.dll" Add-Type -Path "$PSScriptRoot\WebDriver.Support.dll" # Ensure that the ChromeDriver is accessible via PATH or specify the path $chromeDriverPath = "$PSScriptRoot\chromedriver.exe" # Create ChromeDriver service $chromeService = [OpenQA.Selenium.Chrome.ChromeDriverService]::CreateDefaultService($chromeDriverPath) $chromeService.HideCommandPromptWindow = $true $chromeService.SuppressInitialDiagnosticInformation = $true # Create Chrome options $chromeOptions = New-Object OpenQA.Selenium.Chrome.ChromeOptions $chromeOptions.AcceptInsecureCertificates = $true # Initialize ChromeDriver $chromeDriver = New-Object OpenQA.Selenium.Chrome.ChromeDriver($chromeService, $chromeOptions) #Maximized window $chromeDriver.Manage().Window.Maximize() # Navigate to a website $chromeDriver.Navigate().GoToUrl("https://www.google.com") # Enter the username in the Username box $searchBox = $chromeDriver.FindElement([OpenQA.Selenium.By]::Name("q")) Start-Sleep -Seconds 2 #Type on the screen and press submit button $searchBox.SendKeys('vcloud-lab.com') $searchBox.Submit() [void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') [System.Windows.Forms.SendKeys]::SendWait("^{-}") Start-Sleep -Seconds 1 [System.Windows.Forms.SendKeys]::SendWait("^{-}") Start-Sleep -Seconds 1 [System.Windows.Forms.SendKeys]::SendWait("^{-}") Start-Sleep -Seconds 1 # Take the screenshot $screenshot = $chromeDriver.GetScreenshot() # Save the screenshot $screenshot.SaveAsFile("C:\Temp\screenshot.png") Start-Sleep -Seconds 10 # Cleanup $chromeDriver.Quit() |
Download this script Automate_Chrome_Browser_using_PowerShell_Selenium.zip here or it is also available on github.com.
This is the break down of the script, how to use it.
Line 2 and 3: Add and Load type WebDriver.dll and WebDriver.Support.dll system object assembly's into PowerShell.
Line 7: Define path for chromedriver.exe, I am keeping all the files together with script.
Line 10 to 12: Create service for chromedriver.exe and mention configuration example hide command prompt for it.
Line 15 and 16: These are is very crucial lines, I am adding option in the chrome if there is insecure certificate warning it will suppress will not halt at it, It should pass through it.
Line 19 and 22: These are the setting to open chrome browser in full maximized mode.
Line 25: Type this the web URL for the chrome browser, which you want to open and then automate.
Line 28: Once website is loaded, Find the element of Textbox area and provide its name. (To find the elements name click F12 button on the browser to enter into developers tools mode and find / hover over textbox area to find detailed information as shown below screenshot).
Note: I have added pause in between with Start-Stop to give browser loading time for website after each operation is done in the action.
Line 33 and 34: Once Google.com is launched I will autotype this string on the search box to search. and Click to submit to Google Search.
Line 36 to 42: I want to adjust the zoom of the browser, These are not selenium commands.
Line 45 to 48: Using selenium I can take screenshot of the screen and save it to file as well.
Line 52: Once my website automation on the browser is completed, I can close the browser automatically.
You can see loaded chrome browser has message Chrome is being controlled by automated test software.
Useful Articles
PowerShell remoting over HTTPS using self-signed SSL certificate
Configure Powershell WinRM to use OpenSSL generated Self-Signed certificate
Powershell WinRM HTTPs CA signed certificate configuration
Powershell Generate Self-signed certificate with Self-Signed Root CA Signer
PART 1 : POWERSHELL - CREATE LOCAL IIS WEB REPOSITORY FOR DSC MODULE
PART 2 : POWERSHELL - COPY DSC MODULE REMOTELY USING LOCAL WEB REPOSITORY
POWERSHELL CONVERT EXCEL TO DSC (DESIRED STATE CONFIGURATION) CONFIGURATION HASHTABLE FORMAT
Solved Powershell DSC Error A configuration is pending If you are in Pull Mode please runn Update-DscConfiguration
Part 1: Build your first Microsoft PowerShell DSC pull server
Part 2: Generate target server node config for PowerShell DSC pull server
Part 3: Configure PowerShell DSC Local configuration manager LCM in pull mode and update configuration
How to force a PowerShell DSC client to refresh configuration from pull server
Configure a Azure VM with Desired State Configuration in Automation Account
HOW to Add multiple email addresses to Distribution Groups in Exchange online Office 365
Testing domain AD credential with PowerShell one liner