Click here to Skip to main content
15,175,716 members
Articles / Web Development / ASP.NET
Posted 16 Nov 2003


164 bookmarked

Microsoft Web Browser Automation using C#

Rate me:
Please Sign up or sign in to vote.
4.81/5 (88 votes)
16 Nov 20032 min read
An article on axWebBrowser/MSHTML automation using Visual C#.

Sample Image - mshtml_automation.jpg


The Microsoft Web Browser COM control adds browsing, document, viewing, and downloading capabilities to your applications. Parsing and rendering of HTML documents in the WebBrowser control is handled by the MSHTML component which is an Active Document Dynamic HTML (DHTML) Object Model hosting ActiveX Controls and script languages. The WebBrowser control merely acts as a container for the MSHTML component and implements navigations and related functions. MSHTML can be automated using IDispatch and IConnectionPointContainer-style automation interfaces. These interfaces enable a host to automate MSHTML through the object model.


If you are not using the Visual Studio .NET IDE; use Windows Forms ActiveX Control Importer (Aximp.exe) to convert type definitions in a COM type library for an ActiveX control into a Windows Forms control. For instance: to generate the interop DLL's for the ActiveX browser component using the command line run aximp ..\system32\shdocvw.dll relative to your system32 path. Compilation of a form that uses the AxSHDocVw.AxWebBrowser class would be as follows: csc /r:SHDocVw.dll,AxSHDocVw.dll YourForm.cs.

Using the code

Simple Automation scenario:

Image 2

In order to automate this task, first add a Microsoft Web Browser object to an empty C# Windows application. In the Visual Studio .NET IDE, this is done by using the "Customize Toolbox..." context menu (on the Toolbox), pick "Microsoft Web Browser" from the COM components list. This will add an "Explorer" control in the "General" section of the Toolbox.

// navigate to google on Form load
private void Form1_Load(object sender, System.EventArgs e)
    object loc = "<A href=""></A>";
    object null_obj_str = "";
    System.Object null_obj = 0;
    this.axWebBrowser1.Navigate2(ref loc , ref null_obj, 
          ref null_obj, ref null_obj_str, ref null_obj_str);

Next open the solution explorer and add a reference to the Microsoft HTML Object Library (MSHTML) from the COM components list and implement the following code.

// Global variable Task used to prevent recursive code executions.

using mshtml;

private int Task = 1; // global

private void axWebBrowser1_DocumentComplete(object sender, 
         AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e)

        case 1:

            HTMLDocument myDoc = new HTMLDocumentClass();
            myDoc = (HTMLDocument) axWebBrowser1.Document;

            // a quick look at the google html source reveals: 
            // <INPUT maxLength="256" size="55" name="q">
            HTMLInputElement otxtSearchBox = 
               (HTMLInputElement) myDoc.all.item("q", 0);

            otxtSearchBox.value = "intel corp";

            // google html source for the I'm Feeling Lucky Button:
            // <INPUT type=submit value="I'm Feeling Lucky" name=btnI>
            HTMLInputElement btnSearch = 
               (HTMLInputElement) myDoc.all.item("btnI", 0);


        case 2:

            // continuation of automated tasks...




  • Version 1.0 - November 16th 2003 - Original Submission
  • Version 1.1 - November 17th 2003 - Modified axWebBrowser event


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

Alexander Kent
Kentdome LLC
United States United States
Biography in progress Wink | ;-)

Comments and Discussions

QuestionGoogle Instant Search, no more "I'm Feeling Lucky" button Pin
Member 124340813-Apr-16 16:09
MemberMember 124340813-Apr-16 16:09 
GeneralMy vote of 4 Pin
sagu25patel3-Sep-15 21:36
professionalsagu25patel3-Sep-15 21:36 
QuestionHow to manage download file in IE11? Pin
RAHUL RAUT25-Nov-14 21:13
MemberRAHUL RAUT25-Nov-14 21:13 
QuestionHow to block or close popup ads Pin
Sivaji156525-Oct-13 2:48
MemberSivaji156525-Oct-13 2:48 
QuestionGet WebBrowser control from AxWebBrowser object Pin
Sivaji156525-Oct-13 2:36
MemberSivaji156525-Oct-13 2:36 
QuestionGood Pin
masouda6514-Jul-12 18:46
Membermasouda6514-Jul-12 18:46 
Questionsimulate keystrokes for a website Pin
Member 795068825-May-12 6:00
MemberMember 795068825-May-12 6:00 
GeneralMy vote of 5 Pin
sach!!27-Jul-11 3:27
Membersach!!27-Jul-11 3:27 
QuestionMy Vote of 5 Pin
Umesh. A Bhat14-Jul-11 19:45
professionalUmesh. A Bhat14-Jul-11 19:45 
QuestionVery well done! Pin
Omar Gameel Salem13-Jul-11 23:24
professionalOmar Gameel Salem13-Jul-11 23:24 
Generaluses in Web application Pin
arinhere13-Jan-11 19:13
Memberarinhere13-Jan-11 19:13 
QuestionHow to deploy such a program on end users' PC [modified] Pin
milkyjing3-Jan-11 6:53
Membermilkyjing3-Jan-11 6:53 
GeneralMy vote of 4 Pin
oriono1-Oct-10 0:46
Memberoriono1-Oct-10 0:46 
GeneralExecute jQuery Pin
olibara12-May-10 8:06
Memberolibara12-May-10 8:06 
GeneralThank You Pin
Abinash Bishoyi12-Jan-10 21:48
MemberAbinash Bishoyi12-Jan-10 21:48 
QuestionHow to call function of HTML page Pin
anhdungitvn28-Oct-09 23:19
Memberanhdungitvn28-Oct-09 23:19 
AnswerRe: How to call function of HTML page [modified] Pin
zhao wei9-May-10 15:55
Memberzhao wei9-May-10 15:55 
GeneralNot in IE8 though Pin
favazfarook7-Aug-09 18:00
Memberfavazfarook7-Aug-09 18:00 
QuestionHow do I delay until AJAX is finished processing the page? Pin
Member 138216029-Jul-09 13:31
MemberMember 138216029-Jul-09 13:31 
AnswerRe: How do I delay until AJAX is finished processing the page? Pin
binhvtt15-Sep-10 17:54
Memberbinhvtt15-Sep-10 17:54 
Questionjavascript Pin
mnte28-May-09 21:57
Membermnte28-May-09 21:57 
QuestionHow can I load new pages, starting this process over with each one... Pin
trinityAngie14-May-09 4:45
MembertrinityAngie14-May-09 4:45 
QuestionHow to determine when navigation is completed Pin
Ming Liu9-May-09 17:47
MemberMing Liu9-May-09 17:47 
GeneralGood add-on to this article Pin
winmagic200413-Jan-09 10:04
Memberwinmagic200413-Jan-09 10:04 
QuestionHow ot import mshtml and SHDocvw? Pin
YBONG16-Dec-08 15:18
MemberYBONG16-Dec-08 15:18 

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

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