TWebBrowser访问IFrame

//网上资料只提到获取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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值