DO中等待TWebBrowser加载

DO中等待TWebBrowser加载

本文介绍了如何在FOR ... TO ... DO中等待TWebBrowser加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这段代码:

    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;

但是,尽管期望该程序运行,但WebBrowser1仍运行几次。如何不在后台启动WebBrowser1或如何解决?

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?

推荐答案

您需要实现TWebBrowser的3个事件,BeforeNavigate2 ,DocumentComplete和NavigateComplete2

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;

现在您可以使用FDocLoaded变量来知道页面是否已加载到WebBrowser中。

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;

致谢

这篇关于如何在FOR ... TO ... DO中等待TWebBrowser加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:22