Using WatiN and C# to Automate Internet Explorer

I’ve written about web crawling with Perl, but there are some severe limitations to that. The biggest being that it doesn’t (easily) support AJAX. Enter the WatiN web application testing framework for .Net. Best of all, it’s a *free* framework and supports multiple browsers -currently IE, Firefox. I’ve read rumor somewhere that support for Chrome is coming, but I can neither confirm nor deny this.

Getting started with WatiN is amazingly simple.

//launch a new IE browser
using (IE browser = new IE('http://www.example.com')) {
    //now we have access to the browser object
    //filling a textbox and clicking a button is as easy as
    browser.TextField(Find.ByName("some_textbox")).TypeText('foobar');
    browser.Button(Find.ByName("submit_button")).Click();

    //we can also access the full html of the page to perform regex matches, scrapes, etc...
    string fullPageSource = browser.Html;
}

One issues that I ran into was getting a ThreadStateException when the browser was launched. This is quickly remedied by adding the [STAThread] decorator to your programs Main() entry point. More detail is available here.

Hopefully this helps someone out!

2 Responses to "Using WatiN and C# to Automate Internet Explorer"