Microsoft Web browser automation using C #

Source: Internet
Author: User

A codeproject Article directly uses axwebbrowser instead of webbrowser.

Http://www.codeproject.com/KB/cs/mshtml_automation.aspx? FID = 26186 & DF = 90 & MPP = 25 & Noise = 3 & sort = Position & view = Quick & select = 2208064

 

This article introduces the meaning of aximp:

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 runaximp ../system32/shdocvw.dllRelative to your system32 path. Compilation of a form that usesAxSHDocVw.AxWebBrowserClass wocould be as follows:csc /r:SHDocVw.dll,AxSHDocVw.dll YourForm.cs.

 

 

The original article is as follows:

 

Introduction

The Microsoft Web browser COM control adds browsing, document, viewing, and downloading capabilities to your applications. parsing and rendering of HTML documents inWebBrowserControl is handled byMSHTMLComponent which is an active document Dynamic HTML (DHTML) object model hosting ActiveX controls and script ages.WebBrowserControl merely acts as a container forMSHTMLComponent and implements navigations and related functions.MSHTMLCan be automated usingIDispatchAndIConnectionPointContainer-Style automation interfaces. These interfaces enable a host to automateMSHTMLThrough 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 runaximp ../system32/shdocvw.dllRelative to your system32 path. Compilation of a form that usesAxSHDocVw.AxWebBrowserClass wocould 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 "mimize 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.

Collapse copy code
//// 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.

Collapse copy code
//// Global variable Task used to prevent recursive code executions.// using mshtml;private int Task = 1; // globalprivate 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;    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.