There are a number of problems in your code. The counting and wait procedure are really not necessary. The code provided shows you how to detect when the page has completely loaded. The second call to Navigate
is not needed because submitting the form will cause the browser to load the main page.
This code has been tested with the provided site and works :)
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…