Click here to Skip to main content
12,075,783 members (67,518 online)
Click here to Skip to main content
Add your own
alternative version


160 bookmarked

Microsoft Web Browser Automation using C#

, 16 Nov 2003
Rate this:
Please Sign up or sign in to vote.
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:

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 | ;-)

You may also be interested in...

Comments and Discussions

AnswerRe: web automation Pin
amertak19-Apr-09 8:43
memberamertak19-Apr-09 8:43 
QuestionHow to click a javascript object or button? Pin
lalspo7-Nov-08 18:15
memberlalspo7-Nov-08 18:15 
AnswerRe: How to click a javascript object or button? Pin
YBONG16-Dec-08 15:25
memberYBONG16-Dec-08 15:25 
GeneralRe: How to click a javascript object or button? Pin
anhdungitvn28-Oct-09 23:52
memberanhdungitvn28-Oct-09 23:52 
QuestionHow to make automatic download? Pin
Hesham Yassin15-Oct-08 10:09
memberHesham Yassin15-Oct-08 10:09 
Questionthis in c++/clr (pure) Pin
joscha1-Apr-08 11:31
memberjoscha1-Apr-08 11:31 
Generalcookies Pin
maryus_u29-Mar-08 15:00
membermaryus_u29-Mar-08 15:00 
Questionhow can I see the source of last page Pin
etetete26-Mar-08 19:23
memberetetete26-Mar-08 19:23 
AnswerRe: how can I see the source of last page Pin
ravishankar_9-Mar-09 2:03
memberravishankar_9-Mar-09 2:03 
QuestionHow can I get the input in the Frames? Pin
Ben3.Tyo9-Jan-08 21:12
memberBen3.Tyo9-Jan-08 21:12 
Generalopen href Pin
sonic18182-Dec-07 2:56
membersonic18182-Dec-07 2:56 
QuestionHow can i do this Pin
harivinod15-Nov-07 22:00
memberharivinod15-Nov-07 22:00 
GeneralVS2005 changes Pin
dubbele onzin6-Nov-07 4:07
memberdubbele onzin6-Nov-07 4:07 
QuestionI have problems with VS2005 Pin
LeCacho31-Aug-07 6:48
memberLeCacho31-Aug-07 6:48 
AnswerRe: I have problems with VS2005 Pin
LeCacho31-Aug-07 10:24
memberLeCacho31-Aug-07 10:24 
Questionhow to add delay Pin
kzfid25-Jul-07 4:37
memberkzfid25-Jul-07 4:37 
AnswerRe: how to add delay Pin
cambelious31-Jul-07 7:37
membercambelious31-Jul-07 7:37 
QuestionHow to separate cookie? Pin
kzfid23-Jul-07 5:03
memberkzfid23-Jul-07 5:03 
Newsproblem with adding mshtml Pin
VsMAX6669-Jul-07 2:35
memberVsMAX6669-Jul-07 2:35 
GeneralRe: problem with adding mshtml Pin
dmilligan2828-Aug-09 5:16
memberdmilligan2828-Aug-09 5:16 
QuestionCan we Spoof http headers with this control, Does it parse javascript Pin
zeeshan1212-May-07 7:48
memberzeeshan1212-May-07 7:48 
GeneralThx Pin
Diver18219-Apr-07 7:06
memberDiver18219-Apr-07 7:06 
GeneralAutomation of complex DHTML/AJAX applications Pin
Alex Furman17-Mar-07 15:50
memberAlex Furman17-Mar-07 15:50 
Questionhow to disable javascript popup's /error message window... [modified] Pin
kegalle19-Jan-07 0:22
memberkegalle19-Jan-07 0:22 
AnswerRe: how to disable javascript popup's /error message window... Pin
ShajeeDotNet17-Jun-07 1:35
memberShajeeDotNet17-Jun-07 1:35 
Generalclicking href link Pin
vivekbhandari18-Jan-07 3:01
membervivekbhandari18-Jan-07 3:01 
GeneralRe: clicking href link Pin
kegalle18-Jan-07 3:42
memberkegalle18-Jan-07 3:42 
GeneralRe: clicking href link Pin
vivekbhandari18-Jan-07 21:20
membervivekbhandari18-Jan-07 21:20 
GeneralRe: clicking href link Pin
Alexander Kent18-Jan-07 7:19
memberAlexander Kent18-Jan-07 7:19 
GeneralRe: clicking href link Pin
vivekbhandari18-Jan-07 19:27
membervivekbhandari18-Jan-07 19:27 
GeneralRe: clicking href link Pin
Alexander Kent18-Jan-07 21:35
memberAlexander Kent18-Jan-07 21:35 
GeneralRe: clicking href link Pin
vivekbhandari22-Jan-07 19:11
membervivekbhandari22-Jan-07 19:11 
GeneralRe: clicking href link Pin
Alexander Kent22-Jan-07 19:34
memberAlexander Kent22-Jan-07 19:34 
GeneralRe: clicking href link Pin
jposouza27-Jun-07 12:20
memberjposouza27-Jun-07 12:20 
AnswerRe: clicking href link [modified] Pin
Alexander Kent27-Jun-07 14:47
memberAlexander Kent27-Jun-07 14:47 
GeneralRe: clicking href link Pin
Joao Paulo Souza28-Jun-07 8:05
memberJoao Paulo Souza28-Jun-07 8:05 
GeneralRe: clicking href link Pin
donchev_p2-Nov-08 21:33
memberdonchev_p2-Nov-08 21:33 
GeneralGreat Article Pin
kegalle5-Jan-07 0:46
memberkegalle5-Jan-07 0:46 
Questionhow can i listen to an event after clicking a button? Pin
fadil197722-Dec-06 2:02
memberfadil197722-Dec-06 2:02 
QuestionHow to click the link? Pin
wongjinji16-Nov-06 13:02
memberwongjinji16-Nov-06 13:02 
AnswerRe: How to click the link? Pin
Dejan Milic19-Jun-09 14:21
memberDejan Milic19-Jun-09 14:21 
GeneralBrowser has javascript error window pop up.... Pin
adkadkadkadk2-Nov-06 20:31
memberadkadkadkadk2-Nov-06 20:31 
GeneralRe: Browser has javascript error window pop up.... Pin
dmilligan2828-Aug-09 7:25
memberdmilligan2828-Aug-09 7:25 
GeneralFrame Navigation Pin
Dave_Roach1-Jul-06 6:32
memberDave_Roach1-Jul-06 6:32 
GeneralRe: Frame Navigation Pin
nzmike8-Aug-06 18:12
membernzmike8-Aug-06 18:12 
GeneralFrame after Navigation Pin
Dave_Roach1-Jul-06 6:05
memberDave_Roach1-Jul-06 6:05 
QuestionSearch and update Pin
pkruger30-May-06 0:19
memberpkruger30-May-06 0:19 
GeneralMdiChild Pin
teomanersan7-Apr-06 12:07
memberteomanersan7-Apr-06 12:07 
GeneralRe: MdiChild Pin
Bralyan18-Apr-06 11:59
memberBralyan18-Apr-06 11:59 
GeneralBars Pin
vpantaleo13-Feb-06 5:42
membervpantaleo13-Feb-06 5:42 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.160208.1 | Last Updated 17 Nov 2003
Article Copyright 2003 by Alexander Kent
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid