Direct IE COM Automation for Lightweight Web UI Testing

Popular frameworks such as Selenimu, WatiN, and Coded UI all rely on the same underlying mechanism: invoking Internet Explorer’s COM interfaces and manipulating the HTML DOM. Instead of installing a third-party library, you can achieve the same result in a few lines of C# by referencing Microsoft’s own shdocvw.dll and mshtml.dll.

Why drive IE directly?

  • Zero external dependencies—everything ships with Windows or Visual Studio.
  • Fast prototyping: a minimal test harness can be written in minutes.
  • Helps you understand what higher-level tools are doing under the hood.

Adding the required COM references

DLL on disk COM name in "Add Reference" dialog
shdocvw.dll Microsoft Internet Controls (COM tab)
mshtml.dll Microsoft.mshtml (.NET tab)
using SHDocVw;   // InternetExplorer, IWebBrowser2
using mshtml;    // HTMLDocument, HTMLInputElement, etc.

Launching and controlling the browser

var ie = new InternetExplorer
{
    Visible = true,
    Top  = 10,
    Left = 10,
    Height = 800,
    Width  = 1000
};

object missing = Type.Missing;
ie.Navigate("https://www.cnblogs.com", ref missing, ref missing, ref missing, ref missing);
Thread.Sleep(3000);

ie.Navigate("https://www.baidu.com", ref missing, ref missing, ref missing, ref missing);
Thread.Sleep(3000);

ie.GoBack();
Thread.Sleep(1000);
ie.Refresh();
Thread.Sleep(1000);
ie.Quit();

Inspecting the DOM

Use IE’s built-in F12 Developer Tools (or Firebug) to locate element IDs and attributes before scripting them.

Interacting with page elements

Example: perform a search on cnblogs.com.

var ie = new InternetExplorer { Visible = true };
object missing = Type.Missing;
ie.Navigate("https://www.cnblogs.com", ref missing, ref missing, ref missing, ref missing);
Thread.Sleep(3000);

var doc = (HTMLDocument)ie.Document;

var queryBox = (HTMLInputElement)doc.getElementById("q");
queryBox.value = "小坦克";

var searchBtn = (HTMLInputElement)doc.getElementById("btnBloggerSearch");
searchBtn.click();

Common build errors and fixes

If the compiler complains about Microsoft.CSharp.RuntimeBinder.Binder or missing dynamic support, add these references manually to the .csproj file:

<ItemGroup>
  <Reference Include="Microsoft.CSharp" />
  <Reference Include="System.Core" />
</ItemGroup>

After the references are added, rebuild the project and the errors disappear.

Tags: Internet Explorer COM mshtml SHDocVw Web UI automation C# automation

Posted on Mon, 03 Aug 2026 16:25:06 +0000 by cougarreddy