Click here to Skip to main content
15,884,353 members
Articles / Web Development / ASP.NET
Article

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 846.8K   19.5K   164   178
An article on axWebBrowser/MSHTML automation using Visual C#.

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:

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.

C#
//
// 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.

C#
//
// 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


Written By
Kentdome LLC
United States United States
Biography in progress Wink | ;-)

Comments and Discussions

 
GeneralHTMLInputElement Problem Pin
hemlock_04-Dec-04 20:49
hemlock_04-Dec-04 20:49 
GeneralRe: HTMLInputElement Problem Pin
Alexander Kent6-Dec-04 17:05
Alexander Kent6-Dec-04 17:05 
GeneralRe: HTMLInputElement Problem Pin
Alexander Kent6-Dec-04 22:23
Alexander Kent6-Dec-04 22:23 
GeneralLoading more than one page Pin
Anonymous22-Nov-04 6:26
Anonymous22-Nov-04 6:26 
GeneralHTMLInputElement with non-alphanumeric characters Pin
digital_joe0117-Nov-04 8:18
digital_joe0117-Nov-04 8:18 
GeneralRe: HTMLInputElement with non-alphanumeric characters Pin
Alexander Kent20-Nov-04 19:21
Alexander Kent20-Nov-04 19:21 
GeneralScroll the WebBrowser with code Pin
cbranje16-Nov-04 9:52
cbranje16-Nov-04 9:52 
GeneralUsing IHTMLElementCollection Pin
Peter Moss13-Oct-04 10:27
Peter Moss13-Oct-04 10:27 
I see in your example (great article BTW), that you use the IHTMLElementCollection.index(object name, object index) method and you get an element by its name. I am trying to get an element by its index and I can't figure out the right args to use. I am able to iterate over the collection using the IEnumerator interface supported, but in my case, I just want to get the nth element since the elements do not have IDs. I tried this syntax:

children.item(null, n);

but this always seems to return the 0th element in the collection. Any thoughts on this?

Thanks,
Pete


GeneralRe: Using IHTMLElementCollection Pin
BISMOL19-Oct-04 5:15
BISMOL19-Oct-04 5:15 
GeneralDo this using VB.net Pin
DHG249617-Aug-04 13:58
DHG249617-Aug-04 13:58 
GeneralRe: Do this using VB.net Pin
Alexander Kent6-Dec-04 22:14
Alexander Kent6-Dec-04 22:14 
QuestionSimulating hyperlink clicks? Pin
kay.one2-May-04 9:03
kay.one2-May-04 9:03 
AnswerRe: Simulating hyperlink clicks? Pin
Jeff Varszegi2-May-04 14:00
professionalJeff Varszegi2-May-04 14:00 
GeneralRe: Simulating hyperlink clicks? Pin
Heath Stewart21-Jun-04 5:08
protectorHeath Stewart21-Jun-04 5:08 
GeneralHelp: How to control &quot;confirm&quot; popup Pin
sp[a]ce16-Apr-04 23:37
sp[a]ce16-Apr-04 23:37 
GeneralRe: Help: How to control &quot;confirm&quot; popup Pin
Anonymous18-Apr-04 23:11
Anonymous18-Apr-04 23:11 
GeneralMore MSHTML stuff... Pin
JoergKrause19-Jan-04 23:33
JoergKrause19-Jan-04 23:33 
GeneralRe: More MSHTML stuff... Pin
Mlsoun21-Jul-08 15:10
Mlsoun21-Jul-08 15:10 
GeneralRe: More MSHTML stuff... Pin
JoergKrause21-Jul-08 20:23
JoergKrause21-Jul-08 20:23 
GeneralMany Thanks Pin
Joe Pardue2-Jan-04 15:43
Joe Pardue2-Jan-04 15:43 
GeneralTheme support Pin
Member 79393530-Dec-03 23:29
Member 79393530-Dec-03 23:29 
GeneralRe: Theme support Pin
))lance((22-Jan-04 20:17
))lance((22-Jan-04 20:17 
GeneralRe: Theme support Pin
Michael Cleary2-Mar-04 3:57
Michael Cleary2-Mar-04 3:57 
GeneralRe: Theme support Pin
Peter Wone9-Dec-04 16:22
Peter Wone9-Dec-04 16:22 
Generalan example Pin
Ægidius Ahenobarbus30-Nov-03 1:30
Ægidius Ahenobarbus30-Nov-03 1:30 

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.