C # WebBrowser Development Guide

Source: Internet
Author: User

The entire quoted unit mshtml;

Path: C:\windows\assembly\GAC\Microsoft.mshtml\7.0.3300.0__b03f5f7f11d50a3a\Microsoft.mshtml.dll//Different version paths are different


First, how to use WebBrowser to get all the source code of the Web page

1. No frame included

String s= WB1. DocumentText;

2. Contains the framework

IHTMLDocument2 doc=wb1. Document.domdocument as IHTMLDocument2;
IHTMLFramesCollection2 Frames=doc.frames as IHTMLFramesCollection2;
int i_frame=0; 1th Frame
IHTMLWindow2 Frame=frames.item (ref i_frame) as IHTMLWindow2;
IHTMLDocument2 doc2=frame.document as IHTMLDocument2;
IHTMLDocument3 doc3=frame.document as IHTMLDocument3;

strings = Doc2.body.innerHTML;

Second, WebBrowser how to get the elements of the Web page

1. Based on element ID

HtmlElement ele= WB1. document.getElementById ("ID");


2. Depending on the element name

HtmlElement ele= WB1. document.all["Name"];


3. No ID, no name element

3.1 WebBrowser traverse the page elements:

HTMLDocument doc=wb1. Document;

HtmlElementCollection Elements=doc. All;

foreach (htmlelement element in elements)

{

if (element. GetAttribute ("innerText") = = "Submit Answer")//element's Innnertext attribute is "submit Answer"

{

Element.  InvokeMember ("click"); Analog click

Break

}

}

3.2 WebBrowser based on the tag traversal of the element (input, a,img,span,li,div,etc.)

Input: All types that can be entered, such as text box, tick box, check box, drop-down list, picture, etc.

A:A tags, can be linked with hyperlinks

IMG: Picture Type

Span:span type

Li: Drop-down list, list box, etc.

Div:div


HtmlElementCollection eles = WB1. document.getElementsByTagName ("a") as htmlelementcollection;
foreach (HtmlElement ele in Eles)
{
if (ele. GetAttribute ("href")! = null)
if (ele. GetAttribute ("href") = = "Hyperlink to a label")
{
element.  InvokeMember ("click"); Analog Click
Break
}
}

3.3 Index number according to the element

HtmlElement ele= WB1.  DOCUMENT.ALL[0]; A 1th element

HtmlElement ele= WB1.  document.getElementsByTagName ("input") [0]; Element of the first inuput type

3.4 Getting unknown elements based on known elements




C # WebBrowser Development Guide

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.