1

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?

Stas
  • 13
  • 1
  • 4
  • Could you elaborate what *WebBrowser1 run several times* ? And what procedure ? Did you mean some event or where is the code you've posted placed ? Please take the time to update your question since it's unaswerable at this time. – TLama Aug 09 '12 at 18:20
  • I have a some link on loaded page in WebBrowser and need click each in FOR cycle and wait until complete processing 1st link. In Memo1.Lines i have some link. subcatlist thiы is procedure like this code.Оn the site several categories of investments, I collect all attachments – Stas Aug 09 '12 at 18:51

1 Answers1

3

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

cadetill
  • 1,552
  • 16
  • 24
  • 1
    Application.ProcessMessage or Application.ProcessMessages? – Stas Aug 09 '12 at 19:33
  • @TLama this solutions function with a page with frames? – cadetill Aug 10 '12 at 04:57
  • Note that the `OnDocumentComplete` event handler is executed only when the `TWebBrowser` control is displayed (For example, if it is placed inside an inactive tab, it will be executed only at the tab's activation) – Fabrizio Jun 24 '20 at 07:29