Internet Explorer Automation with WatIN

At my workplace I use an IT Service Desk application, called well, "Service Desk" from CA. The system is web-based and provides various queues for Change Orders, Requests and Incidents. Service Desk is used as used as workflow system instead of email. I’m sure this is a pretty common practice for many large IT shops, although they may use a different application. The Service Desk application is one the applications I like to keep open all day, however there is a 60 minute inactivity timeout. By doing any activity within a 60 minute timeframe you avoid timing out, even something simple like clicking an update count button. 
So, I thought I would create a PowerShell that would click an update count button every 50 mintues. I’ve done some scripting using Internet Explorer COM automation and PowerShell. This involves creating an  InternetExplorer.Application object and reading through the page source to find the HTML elements I want to select. I soon discovered issues pragmatically getting to the button through this technique due to Service Desk’s heavy used of nested frames within nested frames. I decided to go another route using WaitINWaitIN Recorder and PowerShell.
Joel Bennet has a post on Using PowerShell and WatiN, which I found helpful. Although I did not use the functions he created, his post provides a quick introduction to PowerShell and WaitIN.  Running WaitIN requires starting PowerShell in STA mode, so this is a PowerShell V2 only script. To start PowerShell in STA mode run:
powershell.exe -STA

Getting Started

  1. Download WatIN
  2. Install WatIN Recorder
Usually working with PowerShell you’ll create an object and explore it’s property and methods, however in this case trying to find the button name was a little difficult. This is where WatIN Recorder helps out. After you’ve installed WatIN Recorder, run as a Administrator and navigate to the URL you want to automate:
Next click the record button and click the HTML element you want to automate. Then stop the WatIN recorder and click copy code to clipboard icon. This will produce some C# code that just needs to be translated into PowerShell:
// Windows
WatiN.Core.IE window = new WatiN.Core.IE();

// Frames
Frame frame_sd_scoreboard = window.Frame(Find.ByName("sd") && Find.ByName("scoreboard"));

// Model
Element __imgBtn0_button = frame_sd_scoreboard.Element(Find.ByName("imgBtn0_button"));

// Code

So, I now know the name of the button and that it is 3 frames deep. A little WatIN object exploration later, I came up with the follow script, which clicks a button every 50 mintues.
#Requires -version 2.0
#powershell.exe -STA

[Reflection.Assembly]::LoadFrom( "$ProfileDirLibrariesWatiN.Core.dll" ) | out-null
$ie = new-object WatiN.Core.IE("")
$scoreboard  = $ie.frames | foreach {$_.frames } | where {$ –eq ‘sd’} |  foreach {$_.frames } | where {$ –eq ‘scoreboard’}
$button = $scoreboard.Element("imgBtn0_button")

while ($true)
    #Sleep for 50 minutes

This entry was posted in PowerShell. Bookmark the permalink.

3 Responses to Internet Explorer Automation with WatIN

  1. veeraiah says:


    I am new to watin tool, I just automated opening the Internet Explorer and entering something in the text box of But IE doesn’t load the add-ons. It loads just one add-on, the windows sign in helper. I have a lot of toolbars that i want it be loaded when IE is opened by the new IE instance

    Is there something i am missing?

  2. shay says:

    this tool is very nice,great potential
    he have
    lots of problems ,not intuitive GUI .
    no right click , no debug , no error / warning way a test stops

Comments are closed.