How wait for TWebBrowser is loaded in FOR … TO … DO?

…衆ロ難τιáo~ 提交于 2020-06-28 11:08:54

问题


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

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