can't get Elements… elements of webbrowser. 'A' and 'INPUT' ando so on… delphi 2007

空扰寡人 提交于 2019-12-06 05:50:17

Your problem lies in the fact that the OnDocumentComplete event will be fired for EACH frameset + the top document. Here is some sample code how to correctly implement this event:

procedure TFrm_browser.BrowserDocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);

var CurrentBrowser: IWebBrowser2;
    TopBrowser: IWebBrowser2;
    Doc :  IHTMLDocument2;

begin
  CurrentBrowser := pDisp as IWebBrowser2;
  TopBrowser := (ASender as TWebbrowser).DefaultInterface;
  if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
   begin
    Doc := CurrentBrowser.Document as IHTMLDocument2;
    if CurrentBrowser = TopBrowser then
     begin
      if Assigned(FOnCompleteDocLoaded) then
       FOnCompleteDocLoaded(Self, Doc);
     end
    else
     begin
      if Assigned(FOnFrameSetLoaded) then
       FOnFrameSetLoaded(Self, Doc);
     end;
   end;
end;

You must process each frameset and the top document.

EDIT

Since the OP does not have a clue, I made a small testproject:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, Mshtml;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
    procedure GetH3Tags(Doc :  IHTMLDocument2);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
 WebBrowser1.Navigate('http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols');
end;

procedure TForm1.GetH3Tags(Doc: IHTMLDocument2);

var Elements: IHTMLElementCollection;
    Element : IHTMLElement;
    Index : Integer;

begin
 Elements := Doc.all.tags('h3') as IHTMLElementCollection;
 Index := Elements.length;
 while Index > 0 do
  begin
   Dec(Index);
   Element := Elements.item(Index, '') as IHTMLElement;
   Memo1.Lines.Add(Element.innerText);
  end;
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
var CurrentBrowser: IWebBrowser2;
    TopBrowser: IWebBrowser2;
    Doc :  IHTMLDocument2;

begin
  CurrentBrowser := pDisp as IWebBrowser2;
  TopBrowser := (ASender as TWebbrowser).DefaultInterface;
  if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
   begin
    Doc := CurrentBrowser.Document as IHTMLDocument2;
    if CurrentBrowser = TopBrowser then
     begin
      // get tags for top level document
       GetH3Tags(Doc);
     end
    else
     begin
      // get tags for each frameset
       GetH3Tags(Doc);
     end;
   end;
end;


end.

DFM file:

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 427
  ClientWidth = 899
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object WebBrowser1: TWebBrowser
    Left = 209
    Top = 0
    Width = 690
    Height = 427
    Align = alClient
    TabOrder = 0
    OnDocumentComplete = WebBrowser1DocumentComplete
    ExplicitLeft = 56
    ExplicitTop = 24
    ExplicitWidth = 300
    ExplicitHeight = 150
    ControlData = {
      4C00000050470000222C00000000000000000000000000000000000000000000
      000000004C000000000000000000000001000000E0D057007335CF11AE690800
      2B2E126208000000000000004C0000000114020000000000C000000000000046
      8000000000000000000000000000000000000000000000000000000000000000
      00000000000000000100000000000000000000000000000000000000}
  end
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 209
    Height = 427
    Align = alLeft
    Color = clHighlight
    TabOrder = 1
  end
end

This sample will get all H3 tags from this page: http://www.w3schools.com/tags/tryit.asp?filename=tryhtml_frame_cols

@Tlama: this is a good example where OnDocumentcomplete will fire multiple times.

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