C # webbrowser Development Guide

Source: Internet
Author: User

C # webbrowser Development Guide

Reference Unit mshtml;

Path: C: \ windows \ assembly \ GAC \ Microsoft. mshtml \ 7.0.3300.0 _ b03f5f7f11d50a3a \ Microsoft. mshtml. dll // different version paths


1. How to Use Webbrowser to obtain the full source code of a webpage

1. framework not included

String s = WB1.DocumentText;

2. Framework included

IHTMLDocument2 doc = WB1.Document. DomDocument as IHTMLDocument2;
IHTMLFramesCollection2 frames = doc. frames as IHTMLFramesCollection2;
Int I _frame = 0; // 1st Frames
IHTMLWindow2 frame = frames. item (ref I _frame) as IHTMLWindow2;
IHTMLDocument2 doc2‑frame.doc ument as IHTMLDocument2;
IHTMLDocument3 doc3‑frame.doc ument as IHTMLDocument3;

Strings = doc2.body. innerHTML;

2. How does webbrowser obtain webpage elements?

1. Based on the element ID

HtmlElement ele = WB1.Document. GetElementById ("ID ");


2. Based on the element Name

HtmlElement ele = WB1.Document. All ["Name"];


3. No ID or Name Element

3.1 webbrowser:

HtmlDocument doc = WB1.Document;

HtmlElementCollection elements = doc. All;

Foreach (HtmlElement element in elements)

{

If (element. GetAttribute ("innerText") = "submit answer") // The innnerText attribute of the element is "submit answer"

{

Element. InvokeMember ("Click"); // simulate a Click

Break;

}

}

3.2 webbrowser traverses the element Tag (input, a, img, span, li, div, etc)

Input: All types that can be entered, such as text box, check box, check box, drop-down list, and image.

A: A tag, which can contain hyperlinks

Img: Image 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") = "A tag HYPERLINK ")
{
Element. InvokeMember ("Click"); // simulate a Click
Break;
}
}

3.3 Based on the index number of the element

HtmlElement ele = WB1.Document. All [0]; // 1st Elements

HtmlElement ele = WB1.Document. GetElementsByTagName ("input") [0]; // The first inuput type element

3.4 obtain unknown elements based on known elements




Related Article

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.