Click here to Skip to main content
15,885,998 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 848.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

 
GeneralGreat Article Pin
kegalle4-Jan-07 23:46
kegalle4-Jan-07 23:46 
Questionhow can i listen to an event after clicking a button? Pin
fadil197722-Dec-06 1:02
fadil197722-Dec-06 1:02 
QuestionHow to click the link? Pin
wongjinji16-Nov-06 12:02
wongjinji16-Nov-06 12:02 
AnswerRe: How to click the link? Pin
Dejan Milic19-Jun-09 13:21
Dejan Milic19-Jun-09 13:21 
GeneralBrowser has javascript error window pop up.... Pin
adkadkadkadk2-Nov-06 19:31
adkadkadkadk2-Nov-06 19:31 
GeneralRe: Browser has javascript error window pop up.... Pin
dmilligan2828-Aug-09 6:25
dmilligan2828-Aug-09 6:25 
GeneralFrame Navigation Pin
Dave_Roach1-Jul-06 5:32
Dave_Roach1-Jul-06 5:32 
GeneralRe: Frame Navigation Pin
nzmike8-Aug-06 17:12
nzmike8-Aug-06 17:12 
Dave,

Did you ever work this out? I'm using the WebBroswer object in .Net 2.0 to try and get access to the Document object of a specific frame so I can then fill in some login fields and get the "login" button clicked. In my code I'm doing this:

webBrowser1.Navigate(www.mysite.com,"loginFrame")

but all I ever get back in the DocumentCompleted handler all I ever get in the Document.Body.InnerHtml is the frameset code for the entire page. In fact I can't see anyway to get access to any of the frames using the webBrowser whatsoever - yet another slightly undercooked MS control it seems!

If you (or anyone else) know how to do this I'd love to know!!

Mike


GeneralFrame after Navigation Pin
Dave_Roach1-Jul-06 5:05
Dave_Roach1-Jul-06 5:05 
QuestionSearch and update Pin
pkruger29-May-06 23:19
pkruger29-May-06 23:19 
GeneralMdiChild Pin
da vinci coder7-Apr-06 11:07
da vinci coder7-Apr-06 11:07 
GeneralRe: MdiChild Pin
Bralyan18-Apr-06 10:59
Bralyan18-Apr-06 10:59 
GeneralBars Pin
vpantaleo13-Feb-06 4:42
vpantaleo13-Feb-06 4:42 
Questionhow 2 work with combo box using axWebBrowser1 ? Pin
vedmack3-Jan-06 22:50
vedmack3-Jan-06 22:50 
AnswerRe: how 2 work with combo box using axWebBrowser1 ? Pin
Bralyan17-Jan-06 4:39
Bralyan17-Jan-06 4:39 
GeneralRe: how 2 work with combo box using axWebBrowser1 ? Pin
vedmack18-Jan-06 6:28
vedmack18-Jan-06 6:28 
GeneralUser Authentication Pin
plclogic13-Dec-05 3:18
plclogic13-Dec-05 3:18 
Generaldrag drop on web browser control Pin
fadi moon9-Dec-05 22:49
fadi moon9-Dec-05 22:49 
Question.Net 2.0 Webbrowser document.designmode = 'On' Pin
phanf8-Dec-05 4:29
phanf8-Dec-05 4:29 
AnswerRe: .Net 2.0 Webbrowser document.designmode = 'On' Pin
ianvink@gmail.com13-Dec-06 13:09
ianvink@gmail.com13-Dec-06 13:09 
GeneralRe: .Net 2.0 Webbrowser document.designmode = 'On' Pin
phanf14-Dec-06 5:19
phanf14-Dec-06 5:19 
GeneralRe: .Net 2.0 Webbrowser document.designmode = 'On' Pin
cmumford4-Jan-07 12:24
cmumford4-Jan-07 12:24 
QuestionHow do I process a second page? Pin
FVCostanza21-Nov-05 2:18
FVCostanza21-Nov-05 2:18 
QuestionRe: How do I process a second page? Pin
anonbogey8-Mar-06 5:15
anonbogey8-Mar-06 5:15 
GeneralThanks Pin
fearless stallion10-Nov-05 0:43
fearless stallion10-Nov-05 0:43 

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.