分享

如何取得网页框架中的内容(300分)

 doc360sir 2011-11-19
查了以前写过的代码,不用在OnDocumentComplete事件也可以做到:

uses   mshtml,   ActiveX;

//   获取框架对象
function   GetFrame(wb:   TWebBrowser;   FrameNo:   Integer):   IWebbrowser2;
var
    OleContainer:   IOleContainer;
    enum:   IEnumUnknown;
    unk:   IUnknown;
    Fetched:   PLongint;
begin
    if   Assigned(wb.Document)   then
    begin
        Fetched   :=   nil;
        OleContainer   :=   wb.Document   as   IOleContainer;
        OleContainer.EnumObjects(OLECONTF_EMBEDDINGS,   Enum);
        Enum.Skip(FrameNo);
        Enum.Next(1,   Unk,   Fetched);
        Result   :=   Unk   as   IWebbrowser2;
    end   else   Result   :=   nil;
end;

procedure   TForm1.Button1Click(Sender:   TObject);
var
    link,   frame:   IHTMLElement;
    coll,   coll1:   IHTMLElementCollection;
    doc:   IHTMLDocument2;
    i,j:   Integer;
begin
    try
        coll   :=   (WebBrowser1.Document   as   IHTMLDocument2).all;
        coll   :=   (coll.tags( 'frame ')   as   IHTMLElementCollection);
        for   i   :=   0   to   coll.Length   -   1   do   begin
            frame   :=   (coll.item(i,   0)   as   IHTMLElement);
            doc   :=   (GetFrame(WebBrowser1,   i).Document   as   IHTMLDocument2);
            //   框架的源地址
            Memo1.Lines.Add(frame.getAttribute(WideString( 'src '),   0));
            //   框架的内容
            Memo1.Lines.Add(doc.Body.innerhtml);
            //   查找‘点击这里’的链接并点击
            coll1   :=   doc.links;
            for   j   :=   0   to   coll1.Length   -   1   do   begin
                link   :=   (coll1.item(j,   0)   as   IHTMLElement);
                if   link.outerText   =   '点击这里 '   then   begin
                    link.Click;
                    Exit;
                end;
            end;
        end;
    except

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

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多