2013-08-01 84 views
2

我想登录(执行一些例行任务)到一个网页(www.soccerproject.com),我无法做到这一点,因为提交按钮类是“superbutton”哪不不有click()处理程序或ID开头。我尝试执行绑定到按钮的onClick方法的JavaScript,但它没有工作,所以这里是我的代码,如果有人可以提供一些帮助,我会很感激。自动登录(webBrowser)

procedure TForm1.Button1Click(Sender: TObject); 
begin 
WebBrowser1.Navigate('http://www.soccerproject.com/spnewl_index.php'); 
end; 

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; 
    const pDisp: IDispatch; var URL: OleVariant); 
var ii:integer ; 
begin 
if (WebBrowser1.LocationURL='http://www.soccerproject.com/spnewl_index.php') and (i<4) then inc(i); 

if i=4 then begin 
    WebBrowser1.OleObject.Document.getElementById('login').setAttribute('value', Edit1.Text); 
    WebBrowser1.OleObject.Document.getElementById('password').setAttribute('value', Edit2.Text); 

    wait(200); 
    WebBrowser1.OleObject.Document.forms[0].submit(); 
    WebBrowser1.Navigate('http://www.soccerproject.com/#'); 
    end; 
end; 

我数到4的原因是,多数民众赞成在web浏览器需要完全加载和显示该网站的时间(能够填补文本)。此外,wait()函数只需等待200毫秒(只是可以肯定)。在此先感谢

回答

3

您的代码中存在一些问题。计数和等待过程实际上不是必需的。所提供的代码显示了如何检测页面何时完全加载。不需要第二次调用Navigate,因为提交表单将导致浏览器加载主页面。 此代码已经过测试,所提供的网站和作品:)

unit u_frm_main; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, OleCtrls, SHDocVw, MsHtml; 

type 
    TForm1 = class(TForm) 
    WebBrowser1: TWebBrowser; 
    Button1: TButton; 
    procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); 
    procedure Button1Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

procedure TForm1.Button1Click(Sender: TObject); 
begin 
WebBrowser1.Navigate('http://www.soccerproject.com/spnewl_index.php'); 
end; 

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant); 

var 
    CurrentBrowser: IWebBrowser2; 
    TopBrowser: IWebBrowser2; 
    Document: OleVariant; 
    Doc3 : IHTMLDocument3; 
    Frm : IHtmlFormElement; 

begin 
CurrentBrowser := pDisp as IWebBrowser2; 
TopBrowser := (ASender as TWebbrowser).DefaultInterface; 
if Assigned(CurrentBrowser) and Assigned(TopBrowser) then 
    begin 
    if CurrentBrowser = TopBrowser then 
    begin 
    Doc3 := CurrentBrowser.Document as IHTMLDocument3; 
    Webbrowser1.OnDocumentComplete := nil; // remove handler to avoid reentrance 
    Doc3.getElementById('login').setAttribute('value', 'SO17999392', 0); 
    Doc3.getElementById('password').setAttribute('value', 'XXXXX', 0); 
    Frm := Doc3.getElementById('indexform') as IHtmlFormElement; 
    if Assigned(Frm) then 
     Frm.submit; 
    end; 
    end; 
end; 

end. 
+0

我会说,如果你导航到'http://www.soccerproject.com/login你不需要这么做。 php'在'Navigate'方法的'PostData'参数中发布数据,例如['这种方式'](http://pastebin.com/Vmt8QYaF)。该登录脚本执行的发布请求由浏览器嗅探并用Delphi 2009进行测试,似乎可行。尽管我仍然想知道是否有一种方法可以优化将参数复制到我的代码中使用的字节变量数组中的难看外观。也许我会问一个关于它的问题。 [+1] – TLama

+0

@Tlama,不错的选择!我相信你可以使用Move()将字节复制到VarArray中? – whosrdaddy

+0

谢谢,我整理出来,现在它确实是我想要的:) –