Click here to Skip to main content
11,790,613 members (63,071 online)
Click here to Skip to main content

Automating Internet Explorer

, 24 Feb 2005 496.1K 5.5K 108
Rate this:
Please Sign up or sign in to vote.
An article describing how to automate IE, mainly for testing purposes.


I attended an ASP.NET Users Group meeting and saw a presentation given by Demetrie Gerodimos. He is an Architect at Dell and was giving a presentation on Test Driven Development. An interesting point of his presentation for me was when he explained how they execute their tests. A discussion with him after the meeting revealed that they were automating Internet Explorer to test their ASPX pages.

After spending some time researching how easily you can automate Internet Explorer in C# (with the help of some URLs given to me by Demetrie), I began writing my own version of IEDriver. The URLs I looked at had a basic example of how to instruct IE to perform various actions. Over the course of the last year, I have enhanced my IEDriver class to handle other types of interactions with IE that I have required. While this is by no means a complete automation class, I believe it performs many of the common functions needed when writing automated tests.


The basic idea here is that we will use Interop services to invoke methods on the COM interfaces built into Internet Explorer. I have written this article because the documentation available on these interfaces is scarce at best. I have spent a lot of time researching on the web and doing trial and error experimentation with these interfaces to produce much of this IEDriver class.

Using the code

The gist of it is a class called IEDriver. This class' constructor will create a new IEXPLORE.EXE process and attach to it. From then on, the methods on the IEDriver class can be used to simulate a user using his browser. While I have not implemented every type of interaction you could have with your browser, I have implemented most of the routines that I have needed to write automated tests for my product.

I have tried to hide the inner workings of the driver from the user of the class. You will notice that there are ClickXXX methods and GetXXX values that will deal with all the COM interfaces to perform the actions necessary or return the value you are interested in.

Here is an example of how easy this class is to use. This example will simply navigate to Google and search for "Automating Internet Explorer":

IEDriver driver = new IEDriver();
driver.SetInputStringValue("q", "Internet Explorer Automation");

In this example, "q" is the name of the input control and "btnG" is the name of the button control on the Google page. Preferably, you will have ID attributes on your HTML elements, but if there is no ID attribute, the driver will find the element with a name attribute.

In order to use this code, you will need to do a couple of things. First of all, you will need to add a couple of references to your project. To do this, right click on References and click Add Reference:

Right click references and select Add Reference

When the Add Reference dialog comes up, first select the .NET tab. Scroll down to the Microsoft.MSHtml object, and click the Select button.

Select the Microsoft.mshtml .Net object

Next, click on the COM tab. Scroll down to the Microsoft Internet Controls object and click the Select button.

Select the Microsoft Internet Controls COM object

Now click on OK. This will add the necessary references to your project that will allow IEDriver class to make the necessary calls to automate IE.

Once you have added the appropriate references to your project, just include the IEDriver in your project. Now, all you have to do is import the IEAutomation namespace into your class and you can begin writing automated tests using IE.

Points of Interest

One thing to keep in mind. The documentation on this stuff is not very good so if you need to enhance IEDriver to support a feature that it currently doesn't, your best bet is probably to use your intellisense to invoke a method that you think might do what you want, and assign the variable to some temporary local variable. Then, fire up your debugger and use the watch window to find out all you can about the objects returned.


While writing my own automated tests, I have discovered that it is useful to extend the IEDriver class and add methods that do tasks that I might perform quite often so that I can use the intellisense and the compiler to eliminate the possibility of mistyping control names. For example:

class MyIEDriver : IEDriver {
    public void ClickSave() {

This gives you compiler checking for things that may normally only be caught at runtime.


  • 2-23-2005: Initial release.

Other Articles

Writing Automated Tests With NUnit and IE.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Leslie Hanks
Web Developer
United States United States
A developer for ScienceTRAX, LLC


You may also be interested in...

Comments and Discussions

GeneralRe: Unspecified exception thrown when trying to find IE Pin
benjiwa26-Apr-07 2:45
memberbenjiwa26-Apr-07 2:45 
GeneralRe: Unspecified exception thrown when trying to find IE Pin
popicul198028-Apr-09 22:41
memberpopicul198028-Apr-09 22:41 
GeneralException thorows Pin
yousefk27-Mar-07 0:27
memberyousefk27-Mar-07 0:27 
GeneralRe: Exception thorows Pin
blottie4-Apr-07 12:14
memberblottie4-Apr-07 12:14 
GeneralAutomation of complex DHTML/AJAX applications Pin
Alex Furman23-Mar-07 7:54
memberAlex Furman23-Mar-07 7:54 
AnswerRe: Automation of complex DHTML/AJAX applications Pin
ZaunCPP28-Oct-08 11:00
memberZaunCPP28-Oct-08 11:00 
GeneralIE Automation Pin
Goku2619-Mar-07 3:52
memberGoku2619-Mar-07 3:52 
GeneralAdded useful functionality... hope this helps. Pin
TriLogic16-Mar-07 5:00
memberTriLogic16-Mar-07 5:00 
I added static methods to the IEDriver class to capture and control an existing Internet Explorer instance. This can be used to control those pesky popup windows. Here is the code - enjoy!

public static IEDriver FromWindow(string title, int delay, int count)
    WindowList wl = new WindowList();
    WindowInfo wi = null;
    count = count > 0 ? count : 1;
    for (int j = 0; j < count && wi == null; j++)
        for (int i = 0; i < wl.Count; i++)
            if (wl[i].title.ToLower().Contains(title.ToLower()))
                wi = wl[i];
        if (delay>0)
    if (wi == null)
        return null;
    IEDriver result = new IEDriver(wi.processID);
    return result;
And the window list class...
public class WindowInfo
    public IntPtr hWnd = IntPtr.Zero;
    public IntPtr lParam = IntPtr.Zero;
    public string title=null;
    public uint   processID=0;
public class WindowList : List<WindowInfo>
    int _hWnd;
    public WindowList(int hWnd)
        : base()
        _hWnd = hWnd;
    public WindowList()
        : base()
        _hWnd = 0;
    public int hWnd
        get { return _hWnd; }
        set { _hWnd = value; }
    public int FindWindow(string windowName, bool wait)
        IntPtr hWnd = WinAPI.FindWindow(null, windowName);
        IntPtr hZip = new IntPtr(0);
        while (wait && hWnd.Equals(hZip))
            hWnd = WinAPI.FindWindow(null, windowName);
        return hWnd.ToInt32();
    public int RefreshChildWindows()
        WinAPI.EnumChildWindows(new IntPtr(_hWnd), ThisEnumWindows, new IntPtr(0));
        return this.Count;
    public int RefreshWindows()
        WinAPI.EnumWindows(ThisEnumWindows, new IntPtr(0));
        return this.Count;
    public int RefreshDesktopWindows()
        return RefreshDesktopWindows(0);
    public int RefreshDesktopWindows(int desktop)
        WinAPI.EnumDesktopWindows(new IntPtr(desktop), ThisEnumWindows, new IntPtr(0));
        return this.Count;
    private bool ThisEnumWindows(IntPtr hWnd, IntPtr lParam)
        int len = WinAPI.GetWindowTextLength(hWnd);
        StringBuilder sb = new StringBuilder(len+1);
        WindowInfo ci = new WindowInfo();
        ci.hWnd = hWnd;
        ci.lParam = lParam;
        WinAPI.GetWindowText(hWnd, sb, sb.Capacity);
        ci.title = sb.ToString();
        if (ci.title.Length > 1)
            if (ci.title.Contains("Microsoft Internet Explorer"))
                WinAPI.GetWindowThreadProcessId(hWnd, out ci.processID);
        return true;
And lastly the WinAPI class
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
public class WinAPI
    #region Wind32API

    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumChildWindows(IntPtr hwndParent, EnumWindowsProc lpEnumFunc, IntPtr lParam);
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
    // For Windows Mobile, replace user32.dll with coredll.dll
    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hWnd, [Out] StringBuilder lpString, int nMaxCount);
    public static extern uint GetWindowThreadProcessId(IntPtr hwnd, out uint lpdwProcessId);
    [DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
    public static extern int GetWindowTextLength(IntPtr hWnd);
    public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumWindowsProc lpEnumFunc, IntPtr lParam);

You know neither the day nor the hour.

GeneralRe: Added useful functionality... hope this helps. Pin
TriLogic16-Mar-07 5:07
memberTriLogic16-Mar-07 5:07 
GeneralRe: Added useful functionality... hope this helps. Pin
NinjaCross28-Jun-07 3:55
memberNinjaCross28-Jun-07 3:55 
GeneralonClick and javascript... Pin
TriLogic28-Feb-07 10:27
memberTriLogic28-Feb-07 10:27 
GeneralRe: onClick and javascript - SOLVED. Pin
TriLogic28-Feb-07 11:59
memberTriLogic28-Feb-07 11:59 
GeneralPopulating a file path Pin
FrankSuero19-Dec-06 9:54
memberFrankSuero19-Dec-06 9:54 
AnswerRe: Populating a file path Pin
shawn4school19-Apr-07 5:55
membershawn4school19-Apr-07 5:55 
GeneralError in VS 2005 Pin
gasbh10-Oct-06 17:47
membergasbh10-Oct-06 17:47 
AnswerRe: Error in VS 2005 Pin
jmd170112-Nov-06 8:44
memberjmd170112-Nov-06 8:44 
GeneralAutomation and SW Explorer Pin
docrob18-Sep-06 4:02
memberdocrob18-Sep-06 4:02 
QuestionGetting Javascript to execute.. Pin
pjbm4-Sep-06 18:07
memberpjbm4-Sep-06 18:07 
GeneralVery useful Pin
cgreen691-Sep-06 4:42
membercgreen691-Sep-06 4:42 
NewsMany of the below things possible Pin
Prashant Goswami30-Aug-06 0:35
memberPrashant Goswami30-Aug-06 0:35 
GeneralRe: Many of the below things possible Pin
TriLogic28-Feb-07 10:22
memberTriLogic28-Feb-07 10:22 
GeneralUseful article Pin
Prashant Goswami28-Aug-06 2:51
memberPrashant Goswami28-Aug-06 2:51 
GeneralAlternative script recording based automation of web applications, etc. Pin
daluu21-Aug-06 12:21
memberdaluu21-Aug-06 12:21 
Questionhow to fill the user/pass fields in the popUp window of the HTACCESS from c# Pin
vedmack3-Aug-06 23:30
membervedmack3-Aug-06 23:30 
AnswerRe: how to fill the user/pass fields in the popUp window of the HTACCESS from c# Pin
daluu21-Aug-06 12:27
memberdaluu21-Aug-06 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1509028.1 | Last Updated 25 Feb 2005
Article Copyright 2005 by Leslie Hanks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid