获取IE (控件)的所有链接(包括Frameset, iframe)

本文介绍两种方法在IE浏览器中获取iframe内部元素列表。一种是通过IHTMLDocument2接口逐层访问iframe,另一种是直接利用IHTMLElement接口进行递归查找。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

IE 顶层 body 节点通过IHTMLElement->get_all 方法无法获取iframe 里面的节点列表

CComPtr < IHTMLElement >  body;
 
CComPtr
< IDispatch >  spDispCollection;
body
-> get_all( & spDispCollection);

所以要获取iframe/frame(frameset) 里面的节点列表的话, 则需要根据body/doc 找到frames, 然后从frames -> IHTMLWindow2 -> IHTMLDocument2 . 主要有2个方法, 下面是代码片段
方法一:
IHTMLDocument2  * pDoc  =  浏览器的Document(IWebBrowser2 -> IDispatch -> IHTMLDocument2); 
IHTMLWindow2 
* pHTMLWnd  =  NULL; 
IHTMLDocument2 
* pFrameDoc = NULL; 
IHTMLFramesCollection2 
* pFramesCollection = NULL; //www.17daima.com
LPDISPATCH lpDispatch; 

long  p; 
VARIANT varindex,varresult; 
varresult.vt
= VT_DISPATCH; 
varindex.vt 
=  VT_I4; 
if (pDoc != NULL) 

    HRESULT hr
= pDoc -> get_frames( & pFramesCollection); 
    
if (SUCCEEDED(hr) && pFramesCollection != NULL) 
    { 
        hr
= pFramesCollection -> get_length( & p); 
        
if (SUCCEEDED(hr)) 
            
for ( int  i = 0 ; i < p; i ++
            { 
                varindex.lVal 
=  i; 
                
if (pFramesCollection -> item( & varindex,  & varresult)  == S_OK) 
                { 
                    lpDispatch
= (LPDISPATCH)varresult.ppdispVal; 
                    
if  (SUCCEEDED(lpDispatch -> QueryInterface(IID_IHTMLWindow2, (LPVOID  * ) & pHTMLWnd))) 
                    { 
                        
if (SUCCEEDED(pHTMLWnd -> get_document(  & pFrameDoc))) 
                        { 
                            
// work with the pFrameDoc 
                        } 
                        pHTMLWnd
-> Release(); //www.17daima.com
                        pHTMLWnd
= NULL; 
                    } 
                } 
            } 
            pFramesCollection
-> Release(); 
    } 
    pDoc
-> Release(); 
}

方法二:
CComQIPtr < IHTMLElement >  pElem  =  ; // 可以递归上面的 CComPtr<IDispatch> spDispCollection 来得到
CComBSTR bstrTagName;
pElem
-> get_tagName( & bstrTagName);
if  ( lstrcmpiW(L " IFRAME " , bstrTagName) == 0   ||
        lstrcmpiW(L
" FRAME " , bstrTagName) == 0  )
{
    CComQIPtr
< IHTMLFrameBase2 >     _framebase2;
    CComPtr
< IHTMLWindow2 >         _framewindow;
    CComPtr
< IHTMLDocument2 >         _framedoc;
    
    
if ( (_framebase2  =  spItem) 
        
&&  SUCCEEDED( _framebase2 -> get_contentWindow( & _framewindow) )  &&  _framewindow != NULL 
        
&&  SUCCEEDED( _framewindow -> get_document( & _framedoc) )  &&  _framedoc != NULL )
    {
        
//  对 _framedoc 节点进行处理
    }
}



附:
IE(控件/接口)中主要有4个部分, Browser, Document, Frame/IFrame, Element , 其对应接口分别是
Browser         -    IWebBrowser2
Document      -    IHTMLDocument2
Frame/IFrame-    IHTMLWindow2
Element         -    IHTMLElement
可以通过下面方法互相获取
browser      -> document        IWebBrowser2::get_Document
document     -> frame           IHTMLDocument2::get_parentWindow
frame        -> document        IHTMLWindow2::get_document
frame        -> parent frame    IHTMLWindow2::get_parent
frame        -> children frames IHTMLWindow2::get_frames
element     -> Frame             IHTMLElement->QI(IHTMLFrameBase2) -> IHTMLFrameBase2->get_contentWindow -> IHTMLWindow2

转自:一起代码网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值