问题
I have a this code:
for i:=0 to Memo1.Lines.Count-1 do
begin
while WebBrowser1.Busy do Application.ProcessMessages;
WebBrowser1.OleObject.Document.Links.item(cat[i]).click;
subcatList;
end;
but WebBrowser1 run several times in spite of the expectations of the procedure. How do I start WebBrowser1 not in the background or what is the solution?
回答1:
You need to implement 3 events of TWebBrowser, BeforeNavigate2, DocumentComplete and NavigateComplete2
TForm1 = class(TForm)
private
CurDispatch: IDispatch;
FDocLoaded: Boolean;
....
procedure TForm1.WebBrowser1BeforeNavigate2(ASender: TObject; const pDisp: IDispatch;
const URL, Flags, TargetFrameName, PostData, Headers: OleVariant;
var Cancel: WordBool);
begin
CurDispatch := nil;
FDocLoaded := False;
end;
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch;
const URL: OleVariant);
begin
if (pDisp = CurDispatch) then
begin
FDocLoaded := True;
CurDispatch := nil;
end;
end;
procedure TForm1.WebBrowser1NavigateComplete2(ASender: TObject; const pDisp: IDispatch;
const URL: OleVariant);
begin
if CurDispatch = nil then
CurDispatch := pDisp;
end;
And now you can use FDocLoaded variable to know if the page is loaded into the WebBrowser
WebBrowser1.Navigate('www.stackoverflow.com');
repeat Application.ProcessMessage until FDocLoaded;
Regards
来源:https://stackoverflow.com/questions/11889398/how-wait-for-twebbrowser-is-loaded-in-for-to-do