Click here to Skip to main content
6,630,289 members and growing! (16,072 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Microsoft Web Browser Automation using C#

By Alexander Kent

An article on axWebBrowser/MSHTML automation using Visual C#.
C++, C#, VB.NET 1.0, .NET 1.1, Win2K, WinXP, Win2003, ASP.NET, MFC, VS.NET2003, Dev
Posted:16 Nov 2003
Views:278,604
Bookmarked:115 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
69 votes for this article.
Popularity: 8.13 Rating: 4.42 out of 5
3 votes, 4.3%
1
4 votes, 5.8%
2
2 votes, 2.9%
3
6 votes, 8.7%
4
54 votes, 78.3%
5

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


Member
Biography in progress ;o)
Occupation: Architect
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 159 (Total in Forum: 159) (Refresh)FirstPrevNext
GeneralHow to call function of HTML page Pinmemberanhdungitvn23:19 28 Oct '09  
GeneralNot in IE8 though Pinmemberfavazfarook18:00 7 Aug '09  
GeneralHow do I delay until AJAX is finished processing the page? PinmemberMember 138216013:31 29 Jul '09  
Questionjavascript Pinmembermnte21:57 28 May '09  
GeneralHow can I load new pages, starting this process over with each one... PinmembertrinityAngie4:45 14 May '09  
GeneralHow to determine when navigation is completed PinmemberMing Liu17:47 9 May '09  
GeneralGood add-on to this article Pinmemberwinmagic200410:04 13 Jan '09  
NewsHow ot import mshtml and SHDocvw? PinmemberYBONG15:18 16 Dec '08  
Generalweb automation Pinmemberahmed_hosni198816:26 5 Dec '08  
AnswerRe: web automation Pinmemberamertak8:43 19 Apr '09  
GeneralHow to click a javascript object or button? Pinmemberlalspo18:15 7 Nov '08  
GeneralRe: How to click a javascript object or button? PinmemberYBONG15:25 16 Dec '08  
GeneralRe: How to click a javascript object or button? Pinmemberanhdungitvn23:52 28 Oct '09  
QuestionHow to make automatic download? PinmemberHesham Yassin10:09 15 Oct '08  
GeneralHTML file parsing Pinmemberrajesh992221:09 10 Aug '08  
Questionthis in c++/clr (pure) Pinmemberjoscha11:31 1 Apr '08  
Generalcookies Pinmembermaryus_u15:00 29 Mar '08  
Generalhow can I see the source of last page Pinmemberetetete19:23 26 Mar '08  
GeneralRe: how can I see the source of last page Pinmemberravishankar_2:03 9 Mar '09  
GeneralHow can I get the input in the Frames? PinmemberBen3.Tyo21:12 9 Jan '08  
Generalopen href Pinmembersonic18182:56 2 Dec '07  
GeneralHow can i do this Pinmemberharivinod22:00 15 Nov '07  
GeneralVS2005 changes Pinmemberdubbele onzin4:07 6 Nov '07  
QuestionI have problems with VS2005 PinmemberLeCacho6:48 31 Aug '07  
AnswerRe: I have problems with VS2005 PinmemberLeCacho10:24 31 Aug '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Nov 2003
Editor: Smitha Vijayan
Copyright 2003 by Alexander Kent
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project