分享

TWebBrowser访问IFrame

 aaie_ 2012-06-28

//网上资料只提到获取Frames的内容,IFrame就没有提及到了。

 

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser: TWebBrowser;
    Button: TButton;
    mmo1: TMemo;
    procedure ButtonClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.ButtonClick(Sender: TObject);
    var
    Tabi:Integer;
    doc:IHTMLDocument2;
    Collection:IHTMLElementCollection;
    Element:IHTMLElement;
    frames:IDispatch;
    framew:IHTMLWindow2;
    i:OleVariant;
    s:string;
    ii,divi:Integer;
    input:IHTMLInputElement;
    docii:HTMLDocument;
    iframe:IWebBrowser;
begin
 
  doc:=Webbrowser.document as IHTMLDocument2;
  i:=4;
  frames:=doc.frames.item(i);
  framew:=frames as IHTMLWindow2;
  docii:=framew.document as HTMLDocument;

  collection:=docii.all.tags('iframe') as IHTMLElementCollection;
  //ShowMessage(IntToStr(Collection.length));
  for ii:= 0 to collection.length -1 do
  begin
    iframe:= Collection.item(ii,varEmpty) as IWebBrowser; //在网上查过不少文章,最后自己尝试成功,关键就是这里。

    doc:=iframe.document as IHTMLDocument2;
    Element:=doc.body as IHTMLElement; 
    Element.innerHTML:=mmo1.Text;
  end;
   //总结:获取Webbrowser的iframe的内容,需要把iframe元索强制转换为iwebbrowser接口,
   //之后,还要强制转换为ihtmlelement接口才可以成功访问iframe里面的元素,否则程序运行出错;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser.Navigate('http://******************');
end;

end.

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多