Delphi checks the validity of getElementById return values

Source: Internet
Author: User

Delphi can use the getElementById method, like JavaScript scripting language, to access elements of the specified ID in a Web page, typically in conjunction with the Twebbrowser component. First use Twebbrowser to browse the Web, and then access the elements of the page, for example:

procedureTform1.button1click (Sender:tobject);
var
Aelement:olevariant;
begin
Aelement: = WebBrowser1.OleObject.Document.GetElementByID (' Btnlogin ');
Aelement.value: =' Login button ';
Aelement.click;
End;

If there is a button with an ID of "btnlogin" in the page, then there is no problem with the above code, and if it does not exist, the setting value and the Click Action on Aelement will throw an exception, so the return value of getElementById should be checked. However, it is very difficult to check the validity of the Olevariant type variable, and by using the VarType function to type-check the aelement, you know that the variable is of the Vardispatch type, so it can be converted to an interface to check whether the return value is valid and the complete code is as follows:

procedureTform1.button1click (Sender:tobject);
var
Aelement:olevariant;
begin
Aelement: = WebBrowser1.OleObject.Document.GetElementByID (' Btnlogin ');
if IDispatch (aelement) <> nil then//check the return value for validity
begin
Aelement.value: = ' login button ';
Aelement.click;
End;
end;

Transferred from: http://www.learnew.com/archives/397.htm

Delphi checks the validity of getElementById return values

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.