The chance of accidentally needs to use TWEBBROWSER, which involves some relaxed things during the development process, so I can't find the search, turn 9CBS post, and finally put things to do, and I have a preliminary understanding for TwebBrowser. At the same time, many friends on the 9CBS also have a lot of questions on TWebBrowser, so they will share their time to share with you. Still, I am very horizontally, please note that if you are not right, please point out, don't be polite, otherwise, not only I have learned something wrong, but also let the people in this article go to astray. Start with nonsense.
First we have to realize that TWEBBROWSER is actually the package of Delphi's Internet Explorer Browser, that is, it is an ActiveX control, and friends who have seen TWebBrowser code will find all methods of this space to call the defaultinterface method, and TWebBrowser This attribute is an object of an IWebBBrowser2 interface type, which is an interface of IE. Most of us use TWebBrowser instead of the IE browser to enable the program to process the page, implement the request for WebApplication, so we first want to get the content broodlable by TWebBrowser. TWEBBROWSER's documnet property is this one. Note that IHTMLDocument2 is defined in the MSHTML unit, and we need to manually add this unit to the USES section. The specific code is as follows:
VAR D: IHTMLDocument2; Begin D: = WebBrowser1.document as htmldocument2;
Let's take a look at what is included in a web page, that is, what we want to process through TWebBrowser. The elements in the web page mainly have ordinary text content, super connection, and dynamic elements (element in the form), of course, there are other elements, but we generally use these in general processing, so I am here with these elements Take an example first I want to introduce the use of form and the use of elements contained in the Form.
There is a Forms properties in the IHTMLDocument interface, this property is the IHTMLELEMENTCOLLECTION interface type, in fact, this Forms property is all FORM elements in the page displayed by TWebBrowser. That is, all FORMs in a page are included in the collection of Forms. We can use htmldocument.forms.Item (Name: Olevariant; Index: Olevariant) to get the form we need to operate, of course, we are just a IDispatch interface, we need to convert this interface into htmlformelement to use form method And attributes. Examples are as follows (this example is Yahoo's free mailbox login interface http://mail.yahoo.com.cn):
Var form: htmlformelement; d: htmldocument2;
begin with WebBrowser1 do begin D: = Document as IHTMLDocument2; Form: = D.Forms.item ( 'login_form', 0) as IHTMLFormElement; (form.item ( 'login', 0) as IHTMLElement) .setAttribute ( 'value' Edit1.text, 0); (Form.Item ('passwd', 0) as htmlelement) .SetaTRibute ('Value', Edit2.Text, 0); //mm.submit; // this Line Work TOO (Form) .item ('. save', 0) as htmlelement) .click; end; end; From above, we can see that we can submit an form through two ways, these two methods are in general, there is no difference, But when you have written some JS to implement the control of the page submit, the former ignores these JS, so the following method is what I recommend.
At this time, I have encountered a question, that is, there are two forms I have to handle, and there is no name in the two forms, that is, form: = D.Forms.Item ('login_form', 0) AS IHTMLFORMELEMENT; this sentence in this sentence we can't get from the web page, and I found a problem when setting this parameter, that is, if there are two FORM elements in the page, the first FORM element can Get it through Item (Varempty, 0), the second FORM element can be obtained by Item (Vernull, 0), and the second parameter of the item does not work, this problem may be due to my error use of the function. I hope some people can give this problem solution (I will find the answer in my own post, it is indeed, the first parameter should be the object we want to use. The index value.). My idea is that DHTML automatically generates a unique element name when not clearly gets a name, but how to get this unique element name? This is just an idea, we will see that when we handle the link, we have to encounter this problem. Examples are as follows (
This is a JSP program I do myself): Var form: htmlformElement; D: htmldocument2; begin with webbrowser1 do beg, d: = document as htmldocument2; form: = D.Forms.Item (Varnull, 01) as htmlformelement; .item ('firstname', 0) as htmlelement) .SetAttribute ('Value', Edit1.Text, 0); (Form.Item ('Lastname', 0) as htmlelement) .SetaTribute ('Value', Edit1.Text , 0); form.submit; end;
The above is the basic processing of FORM in the web page. Next I introduce the connection to the link in the web page, we generally want to enable the program to automatically click on the connection. As mentioned earlier here, I can only get the first two no name connections. Examples are as follows: var Links: IHTMLElementCollection; D: IHTMLDocument2; Element: IHTMLElement; begin with WebBrowser1 do begin D: = Document as IHTMLDocument2; Links: = D.links; Element: = (Links.Item (varempty, 0) as IHTMLElement) ShowMessage (Element.getaTribute ('href', 0)); Element: = (Links.Item (0) as htmlelement; showMessage ('href', 0)); end; end;
We can simulate click by calling the Element.Click event in the above code. No, you can't write, there is a common question that is how to write Browser to open a new window when you open a new window. This is to change PPDISP in NewWindows2 in TWebBrowser.
It's so much, I hope everyone can learn from middle school, and I hope someone can answer my question above.
Copyright: IDILENT website reproduced please indicate the author's other reprint, please contact the author (iDilent@yahoo.com.cn).