Click here to Skip to main content
15,889,096 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 853.1K   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

 
AnswerRe: how can I see the source of last page Pin
ravishankar_9-Mar-09 1:03
ravishankar_9-Mar-09 1:03 
QuestionHow can I get the input in the Frames? Pin
Ben3.Tyo9-Jan-08 20:12
Ben3.Tyo9-Jan-08 20:12 
Generalopen href Pin
sonic18182-Dec-07 1:56
sonic18182-Dec-07 1:56 
QuestionHow can i do this Pin
harivinod15-Nov-07 21:00
harivinod15-Nov-07 21:00 
GeneralVS2005 changes Pin
dubbele onzin6-Nov-07 3:07
dubbele onzin6-Nov-07 3:07 
QuestionI have problems with VS2005 Pin
LeCacho31-Aug-07 5:48
professionalLeCacho31-Aug-07 5:48 
AnswerRe: I have problems with VS2005 Pin
LeCacho31-Aug-07 9:24
professionalLeCacho31-Aug-07 9:24 
Questionhow to add delay Pin
kzfid25-Jul-07 3:37
kzfid25-Jul-07 3:37 
For example,

int nTask = 1; \\global var

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
switch(nTask)
{
case 1:
//do something here
<- Add "System.Threading.Thread.Sleep(10000);"
nTask += 1;
break;
case 2:
//do something here
<- Add "System.Threading.Thread.Sleep(10000);"
nTask -= 1;
break;
}
}

It seems ok, but if I want to add some delay here by using "System.Threading.Thread.Sleep(10000)", the problem comes.

The whole windows will get occupied, you cannot move it and do anything on the webpages, such as click a button. I know that it is due to the current thread is holded by "System.Threading.Thread.Sleep(10000);", and the window can not response you until it is finished.

But if I cannot add delay here, where can I add the delay to make the same effect but no the problem described above?

Any help will be highly appreciated. Thanks
AnswerRe: how to add delay Pin
cambelious31-Jul-07 6:37
cambelious31-Jul-07 6:37 
QuestionHow to separate cookie? Pin
kzfid23-Jul-07 4:03
kzfid23-Jul-07 4:03 
Newsproblem with adding mshtml Pin
VsMAX6669-Jul-07 1:35
VsMAX6669-Jul-07 1:35 
GeneralRe: problem with adding mshtml Pin
dmilligan2828-Aug-09 4:16
dmilligan2828-Aug-09 4:16 
QuestionCan we Spoof http headers with this control, Does it parse javascript Pin
zeeshan1212-May-07 6:48
zeeshan1212-May-07 6:48 
GeneralThx Pin
Diver18219-Apr-07 6:06
Diver18219-Apr-07 6:06 
GeneralAutomation of complex DHTML/AJAX applications Pin
Alex Furman17-Mar-07 14:50
Alex Furman17-Mar-07 14:50 
Questionhow to disable javascript popup's /error message window... [modified] Pin
kegalle18-Jan-07 23:22
kegalle18-Jan-07 23:22 
AnswerRe: how to disable javascript popup's /error message window... Pin
ShajeeDotNet17-Jun-07 0:35
ShajeeDotNet17-Jun-07 0:35 
Generalclicking href link Pin
vivekbhandari18-Jan-07 2:01
vivekbhandari18-Jan-07 2:01 
GeneralRe: clicking href link Pin
kegalle18-Jan-07 2:42
kegalle18-Jan-07 2:42 
GeneralRe: clicking href link Pin
vivekbhandari18-Jan-07 20:20
vivekbhandari18-Jan-07 20:20 
GeneralRe: clicking href link Pin
Alexander Kent18-Jan-07 6:19
Alexander Kent18-Jan-07 6:19 
GeneralRe: clicking href link Pin
vivekbhandari18-Jan-07 18:27
vivekbhandari18-Jan-07 18:27 
GeneralRe: clicking href link Pin
Alexander Kent18-Jan-07 20:35
Alexander Kent18-Jan-07 20:35 
GeneralRe: clicking href link Pin
vivekbhandari22-Jan-07 18:11
vivekbhandari22-Jan-07 18:11 
GeneralRe: clicking href link Pin
Alexander Kent22-Jan-07 18:34
Alexander Kent22-Jan-07 18:34 

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.