Some days ago, WebBrowser in VS2005 was used to control the elements in the webpage. Although VS2005 encapsulated many good functions, it was always lacking in use. For example, if I want to obtain the source code of the webpage on the Framework page, I cannot find a proper method.
MSHTML is a COM component of Microsoft. It encapsulates all elements and their attributes in the HTML language and can access all elements of a specified Webpage through the standard interface provided by MSHTML. the MSHTML object model consists of objects and sets. HTML at the root of the page describes a window that opens the page, including a series of sets and objects. Such as the Frames set, History, Location, Navigator, Document, Vi-sum, and Event object. The Document object is used to describe the actual webpage in the customer window. It consists of a series of attributes, methods, objects, and sets. The All set contains All Tag elements in the webpage. Its main methods and attributes include:
(1) Length (Length): indicates the number of tags that appear. You can refer to the set of tags as a one-dimensional array starting from 0. The order of tags is arranged in the webpage position;
(2) Tags (TAG): used to filter the set of given Tags, such as Doc. Al1.Tags (P) to get all the segment Tags P;
(3) Item: used to select an element in the set, such as object. item (0) gets 1st elements of the Set, and object. item (I) obtains the I + 1 element.
In addition, IHTMLElement is a common set object that represents the set of specified tags in the webpage. Through this set object, you can obtain the content of specific tags on the webpage. IHTMLElement has four main attributes:
(1) InnerText: the text between the start mark and the end mark;
(2) InnerHTML: Text and HTML between the start tag and the end tag;
(3) OuterText: Object text;
(4) OuterHTML: Object text and HTML.
Automatic submission
Procedure tform1.button1click (Sender: tobject ); VaR DOC: ihtmldocument2; Input: olevariant; Userinputelement, pwdinputelement: ihtmlinputelement; Begin DOC: descriwebbrowser1.doc ument as ihtmldocument2; Userinputelement: = (Doc. All. Item ('user' (that is, the name of the user name control in the webpage), 0) as ihtmlinputelement ); Userinputelement. Value: = edit1.text; (that is, what you want to input to the webpage) Pwdinputelement: = (Doc. All. Item ('Password', 0) as ihtmlinputelement ); Pwdinputelement. Value: = edit2.text; Input: = Doc. All. Item ('submit ', 0 ); Input. Click; End; |
When the data submission button does not have the name attribute, use the following method:
procedure TForm1.Button1Click(Sender: TObject); var Doc:IHTMLDocument2; form:ithmlformelement; userinputelement,pwdinputelement:ihtmlinputelement; Begin Doc: descriwebbrowser1.doc ument as ihtmldocument2; Userinputelement: = (doc. all. item ('user' (that is, the name of the user name control in the webpage), 0) as ihtmlinputelement ); Userinputelement. value: = edit1.text; (that is, what you want to input to the webpage) Pwdinputelement: = (doc. all. item ('Password', 0) as ihtmlinputelement ); Pwdinputelement: = edit2.text; Form: = (doc. all. item ('login _ Form', 0) as ihtmlformelement ): Form. submit; End; |
The following method is used when the current page is a framework page:
procedure TForm1.Button1Click(Sender: TObject); var doc2:IHTMLDocument2; o : Olevariant; ole_index: OleVariant; frame_dispatch: IDispatch; frame_win: IHTMLWindow2; frame_doc: IHTMLDocument2; begin begin doc2 := WebBrowser1.Document as IHTMLDocument2; ole_index:=0; frame_dispatch := doc2.Frames.Item(ole_index); if frame_dispatch <> nil then begin frame_win := frame_dispatch as IHTMLWindow2; frame_doc := frame_win.document; // memo1.lines.add(IHTMLDocument2(frame_doc).body.outerHTML); End; end; |