Pressing Buttons on a web page via Delphi [duplicate]

五迷三道 提交于 2020-01-15 04:28:06

问题


Ex1: WebBrowser.OleObject.Document.GetElementByID('ID HERE').Click;

Ex2: < input type="submit" VALUE="Login" >

The above two examples are for pressing buttons on web pages via Delphi. Ex2 works well on various web sites but not all. Is this because Ex2 only works on HTML buttons? I tried Ex1 but some code is missing, when I try it, I get a message saying 'Object or class type required'. Also Ex1 has no example code, can anyone fill me in on why I get this message and put some code up for Ex1 please.


回答1:


I got this code from: MrBaseball34 at delphipages It didn't work initially because I wrote 'WebBrowser' instead of 'WebBrowser1'. But it works perfectly. Here is the code:

procedure TForm1.Button1Click(Sender: TObject);  
var  
x: integer;  
thelink: OleVariant; 

begin  
thelink:= WebBrowser1.OleObject.Document.all.tags('A');  
if thelink.Length > 0 then  
  begin  
  for x := 0 to thelink.Length-1 do  
    begin  
    if Pos('put id string here', thelink.Item(x).id) > 0 then
      begin  
        thelink.Item(x).click;  
        Break;
      end;
    end;
  end;
end;



回答2:


When working with TWebBrowser, and COM/ActiveX objects in general, it's really handy to know the difference between late binding and early binding. If you use OleVariant variables, have them refer to 'live' object, and use the dot operator (.) to invoke methods and properties, they get resolved at run-time. They are late bound as opposed to early binding where you would use specific interfaces.

Include unit MSHTML in the uses clause, and then use IHTMLDocument3(WebBrowser1.Document), and the different interfaces defined by MSHTML, such as IHTMLElementand IHTMLAnchorElement. You will find that you also get code completion up to some point, but also that you might need an extra cast between things like IHTMLElement and IHTMLElement2 with the asoperator.




回答3:


There may be any kind of error. Like misspelled ID or wrong datatype missing interface you hope to use, or lacking some item and returning nil instead.

Problem with long lines like WebBrowser.OleObject.Document.GetElementByID('ID HERE').Click; is that you can hardly tell in which place the error occured. And sometimes it is not easy to check intermediate values and its properties. There are a lot of innate expectations coded in such long lines, and you can hardly detect which one was failed.

When you meet errors in such long lines, you'd better split them at tiny action items - the ol' good divide and conquer principle. Declare few variables and split this long complex line into multiple simplistic ones.

var0 := WebBrowser;
var1 := var0.OleObject; 
var2 := var1.Document; 
var3 := var2.GetElementByID('ID HERE'); 
var3.Click;

Tracing this, executing one line a time, you can check which values and data types would be issued at each traversing step.



来源:https://stackoverflow.com/questions/13066692/pressing-buttons-on-a-web-page-via-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!