Click here to Skip to main content
Click here to Skip to main content

Microsoft Web Browser Automation using C#

By , 16 Nov 2003
 

Sample Image - mshtml_automation.jpg

Introduction

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.

Note

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="http://www.google.com/">http://www.google.com/</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)

{
switch(Task)
    {
        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);
            btnSearch.click();

            Task++;
            break;

        case 2:

            // continuation of automated tasks...
            break;
    }
}

References

MSDN

History

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

License

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
Member
Biography in progress Wink | ;-)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGood Pinmembermasouda6514 Jul '12 - 17:46 
Questionsimulate keystrokes for a website PinmemberMember 795068825 May '12 - 5:00 
GeneralMy vote of 5 Pinmembersach!!27 Jul '11 - 2:27 
QuestionMy Vote of 5 PinmemberUmesh. A Bhat14 Jul '11 - 18:45 
QuestionVery well done! PinmemberOmar Gamil13 Jul '11 - 22:24 
Generaluses in Web application Pinmemberarinhere13 Jan '11 - 18:13 
QuestionHow to deploy such a program on end users' PC [modified] Pinmembermilkyjing3 Jan '11 - 5:53 
GeneralMy vote of 4 Pinmemberoriono30 Sep '10 - 23:46 
GeneralExecute jQuery Pinmemberolibara12 May '10 - 7:06 
GeneralThank You PinmemberAbinash Bishoyi12 Jan '10 - 20:48 
QuestionHow to call function of HTML page Pinmemberanhdungitvn28 Oct '09 - 22:19 
GeneralNot in IE8 though Pinmemberfavazfarook7 Aug '09 - 17:00 
QuestionHow do I delay until AJAX is finished processing the page? PinmemberMember 138216029 Jul '09 - 12:31 
Questionjavascript Pinmembermnte28 May '09 - 20:57 
QuestionHow can I load new pages, starting this process over with each one... PinmembertrinityAngie14 May '09 - 3:45 
QuestionHow to determine when navigation is completed PinmemberMing Liu9 May '09 - 16:47 
GeneralGood add-on to this article Pinmemberwinmagic200413 Jan '09 - 9:04 
QuestionHow ot import mshtml and SHDocvw? PinmemberYBONG16 Dec '08 - 14:18 
Generalweb automation Pinmemberahmed_hosni19885 Dec '08 - 15:26 
QuestionHow to click a javascript object or button? Pinmemberlalspo7 Nov '08 - 17:15 
QuestionHow to make automatic download? PinmemberHesham Yassin15 Oct '08 - 9:09 
GeneralHTML file parsing Pinmemberrajesh992210 Aug '08 - 20:09 
Questionthis in c++/clr (pure) Pinmemberjoscha1 Apr '08 - 10:31 
Generalcookies Pinmembermaryus_u29 Mar '08 - 14:00 
Questionhow can I see the source of last page Pinmemberetetete26 Mar '08 - 18:23 
QuestionHow can I get the input in the Frames? PinmemberBen3.Tyo9 Jan '08 - 20:12 
Generalopen href Pinmembersonic18182 Dec '07 - 1:56 
QuestionHow can i do this Pinmemberharivinod15 Nov '07 - 21:00 
GeneralVS2005 changes Pinmemberdubbele onzin6 Nov '07 - 3:07 
QuestionI have problems with VS2005 PinmemberLeCacho31 Aug '07 - 5:48 
Questionhow to add delay Pinmemberkzfid25 Jul '07 - 3:37 
QuestionHow to separate cookie? Pinmemberkzfid23 Jul '07 - 4:03 
Newsproblem with adding mshtml PinmemberVsMAX6669 Jul '07 - 1:35 
QuestionCan we Spoof http headers with this control, Does it parse javascript Pinmemberzeeshan1212 May '07 - 6:48 
GeneralThx PinmemberDiver18219 Apr '07 - 6:06 
GeneralAutomation of complex DHTML/AJAX applications PinmemberAlex Furman17 Mar '07 - 14:50 
Questionhow to disable javascript popup's /error message window... [modified] Pinmemberkegalle18 Jan '07 - 23:22 
Generalclicking href link Pinmembervivekbhandari18 Jan '07 - 2:01 12 
GeneralGreat Article Pinmemberkegalle4 Jan '07 - 23:46 
Questionhow can i listen to an event after clicking a button? Pinmemberfadil197722 Dec '06 - 1:02 
QuestionHow to click the link? Pinmemberwongjinji16 Nov '06 - 12:02 
GeneralBrowser has javascript error window pop up.... Pinmemberadkadkadkadk2 Nov '06 - 19:31 
GeneralFrame Navigation PinmemberDave_Roach1 Jul '06 - 5:32 
GeneralFrame after Navigation PinmemberDave_Roach1 Jul '06 - 5:05 
QuestionSearch and update Pinmemberpkruger29 May '06 - 23:19 
GeneralMdiChild Pinmemberteomanersan7 Apr '06 - 11:07 
GeneralBars Pinmembervpantaleo13 Feb '06 - 4:42 
Questionhow 2 work with combo box using axWebBrowser1 ? Pinmembervedmack3 Jan '06 - 22:50 
GeneralUser Authentication Pinmemberplclogic13 Dec '05 - 3:18 
Generaldrag drop on web browser control Pinmemberfadi moon9 Dec '05 - 22:49 

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

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