Mirror

How to list all links of a page of a TWebbrowser.txt (Views: 100)


Problem/Question/Abstract:

How to list all links of a page of a TWebbrowser

Answer:

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Webbrowser1.OleObject.Document.links.Length - 1 do
    Listbox1.Items.Add(Webbrowser1.OleObject.Document.Links.Item(i));
end;

{*****************}

{ if there are frames }

procedure TForm1.Button2Click(Sender: TObject);
var
  u: variant;
  v: IDispatch;
  s: string;

  procedure RecurseLinks(htmlDoc: variant);
  var
    BodyElement: variant;
    ElementCo: variant;
    HTMLFrames: variant;
    HTMLWnd: variant;
    j, i: integer;
  begin
    if VarIsEmpty(htmlDoc) then
      exit;
    BodyElement := htmlDoc.body;
    if BodyElement.tagName = 'BODY' then
    begin
      ElementCo := htmlDoc.links;
      j := ElementCo.Length - 1;
      for i := 0 to j do
      begin
        u := ElementCo.item(i);
        s := u.href;
        listLinks.Items.Add(s);
      end;
    end;
    HTMLFrames := htmlDoc.Frames;
    j := HTMLFrames.length - 1;
    for i := 0 to j do
    begin
      HTMLWnd := HTMLFrames.Item(i);
      RecurseLinks(HTMLWnd.Document);
    end;
  end; // RecurseLinks
begin
  v := WebBrowser1.document;
  listLinks.Clear;
  RecurseLinks(v);
end;

<< Back to main page