Powershell Chrome browser automation

One of the most useful benefits of learning a scripting language like PowerShell is the ability to automate tasks.  There are some tasks that we do regularly.  It is useful to automate such repetitive tasks and actions.

A browser is a software that we use regularly. More often than not we end up browsing the same website again. And also sometimes we perform the same activity on the websites.

Our life becomes much easier if we automate the process using PowerShell scripts. In this article, we will see step by step instructions on how to automate website search in Google Chrome using PowerShell.

Setting up Selenium:

For achieving Chrome browser automation we need to use a dll called Selenium. Alongside the dll we also need a chromedriver.exe. Follow the below steps to see how to download and set up the necessary files for Chrome automation.

  1. Go to selenium website using the below link.
    Web Link
    https://www.selenium.dev/downloads/
  2. Click on the download button shown in the image.
    Download Selenium
    Download Selenium
  3. Extract the RAR file.
    extract the RAR file
    extract the RAR file
  4. Right-click the file and select rename.
  5. Rename the file with the .zip extension.
    rename to ZIP file
    rename to ZIP file
  6. Right-click the file and select extract files.
    extract the zip file
    extract the zip file
  7. Come to the location shown in the image.
    come to the location in the zip file
    come to the location in the zip file
  8. Copy the DLL and place it in the destination folder.
    place the dll in the destination
    place the dll in the destination
  9. Use the below link to download the Chrome driver.
    Web Link
    https://sites.google.com/a/chromium.org/chromedriver/downloads
    download appropriate chrome driver
    download appropriate chrome driver
  10. Download the driver that is appropriate to the Chrome version.
  11. You can check the Chrome version using the link given below.
    Web Link
    chrome://version/
    check chrome version
    check chrome version
  12. Extract Chrome driver zip file.
    extract the file
    extract the file
  13. Copy the chromedriver.exe to the same destination folder.
    place the exe in the destination
    place the exe in the destination
  14. Once done we will have the necessary dll and the chromedriver.exe in our destination folder as shown in the image.

PowerShell browser automation scripts:

  1. Import the dll file using the full path of the dll file.
    Script
    Import-Module E:\pwrshl\TkChromeAutomation\WebDriver.dll
    import the dll
    import the dll
  2. Create an instance of Chrome using the below script.
    Script
    $ChromeWindow = New-Object OpenQA.Selenium.Chrome.ChromeDriver
    create a chrome window
    create a chrome window
  3. We can see the automated test software.
    automated chrome window
    automated chrome window
  4. Navigate to the desired website using this script.
    Script
    $ChromeWindow.Navigate().GoToURL(‘https://teckangaroo.com’)
    navigate to website
    navigate to website
    navigated to the site
    navigated to the site
  5. Right-click and select inspect element.
    select inspect element
    select inspect element
  6. Scroll down the page to right click on the search box and select inspect element.
    inspect element the search box
    inspect element the search box
  7. Right click the element in the developer tools and click copy path select copy X path.
    copy the xpath of search box
    copy the xpath of search box
  8. Enter script with the copied path and the search term as shown in the image.
    Script
    $ChromeWindow.FindElementByXPath(‘//*[@id=”search-2″]/form/label/input’).SendKeys(‘powershell’)
    search for powershell
    search for powershell
  9. We can see that the search term has entered into the search box as expected.
    PowerShell in search term
    PowerShell in the search term
  10. Enter the below script rail press the enter key.
    Script
    $ChromeWindow.FindElementByXPath(‘//*[@id=”search-2″]/form/label/input’).SendKeys([OpenQA.Selenium.Keys]::Enter)
    Press the enter key
    Press the enter key
  11. We can see that it has carried out the search for the term PowerShell.
    search for PowerShell
    search for PowerShell

We have automated the process of using the Chrome browser in PowerShell for the mentioned task.

Verdict:

In this article, we learn how to set up selenium and chromedriver.exe file to use PowerShell and automate the Chrome browser. We have given step by step instructions on how to use PowerShell to automate the Chrome browser.

If you have any doubts please ask us in the comments below.

Grab the latest information related to PowerShell, Microsoft Teams, and other technologies at TecKangaroo.

Leave a Comment