Previous     Next

Automate FireFox with ASP.net

Usually, if you are writing in c# or vb.net then when you want to automate a browser you would use the MS WebBrowser control (AxWebBrowser). However, that control is missing some attributes (eg - you can’t set the referrer), but Firefox gives you the lot.

All the hard work is done by the SWAT (Simple Web Automation Toolkit) library (http://ulti-swat.wiki.sourceforge.net/). Download the SWAT.dll and add a reference. You also have to add the file Interop.SHDocVw.dll to the same directory and also add a reference.

A quick demo - get the first result from Google for a search term:


                Dim sResult As String
                sResult = GoogleFirstResult("web hosting")

        Private Function GoogleFirstResult(ByVal sSearchTerm As String) As String
                Dim oFireFoxBrowser As SWAT.WebBrowser

                oFireFoxBrowser = New SWAT.WebBrowser(BrowserType.FireFox)
                oFireFoxBrowser.OpenBrowser()

                oFireFoxBrowser.NavigateBrowser("http://google.com")

                oFireFoxBrowser.SetElementAttribute(IdentifierType.Name, "q", "value", sSearchTerm)

                oFireFoxBrowser.StimulateElement(IdentifierType.Name, "btnI", "onclick")

                'Give the browser a chance to get to the site.
                oFireFoxBrowser.Sleep(1000)

                GoogleFirstResult = oFireFoxBrowser.CurrentLocation

        End Function

This post brought to you by WeGotDomain.com - Over 10,000 Aged domains for sale!

Related posts:

  1. SEOMoz Linkscape API wrapper in VB.Net

If you liked this, then subscribe to my RSS feed