3

ウェブページ (www.soccerproject.com) にログインしようとしています (いくつかの日常的なタスクを実行するため) ID を開始します。ボタンの onClick メソッドにバインドされた JavaScript を実行しようとしましたが、うまくいきませんでした。ここに私のコードを示します。

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 にカウントする理由は、webBrowser が Web サイトを完全にロードして表示する (テキストを入力できるようにする) 必要があるためです。また、wait() 関数は単純に 200 ミリ秒待機します (念のため)。前もって感謝します

4

1 に答える 1

4

コードには多くの問題があります。カウントと待機手順は実際には必要ありません。提供されているコードは、ページが完全にロードされたことを検出する方法を示しています。Navigateフォームを送信するとブラウザがメイン ページをロードするため、2 回目の呼び出しは必要ありません。このコードは、提供されたサイトでテストされ、動作します:)

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.
于 2013-08-02T07:27:33.390 に答える