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