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