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

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; 
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(); 
                        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 节点进行处理
    }
}

iframe 跨域访问(cross frame)  zz from : http://codecentrix.blogspot.com/2007/10/when-ihtmlwindow2getdocument-returns.html 
由于安全性限制, 为防止跨域脚本攻击, 当frames 跨域的时候, IHTMLWindow2::get_document 调用将返回 E_ACCESSDENIED . 
下面函数 HtmlWindowToHtmlDocument  对于跨域的frame 通过 IHTMLWindow2 -> IID_IWebBrowserApp -> IHTMLWindow2 绕过了限制.

// Converts a IHTMLWindow2 object to a IHTMLDocument2. Returns NULL in case of failure.
// It takes into account accessing the DOM across frames loaded from different domains.
CComQIPtr<IHTMLDocument2> HtmlWindowToHtmlDocument(CComQIPtr<IHTMLWindow2> spWindow)
{
     ATLASSERT(spWindow != NULL);

CComQIPtr<IHTMLDocument2> spDocument;
     HRESULT hRes = spWindow->get_document(&spDocument);
    
     if ((S_OK == hRes) && (spDocument != NULL))
     {
          // The html document was properly retrieved.
          return spDocument;
     }

// hRes could be E_ACCESSDENIED that means a security restriction that
     // prevents scripting across frames that loads documents from different internet domains.
     CComQIPtr<IWebBrowser2>  spBrws = HtmlWindowToHtmlWebBrowser(spWindow);
     if (spBrws == NULL)
     {
          return CComQIPtr<IHTMLDocument2>();
     }

// Get the document object from the IWebBrowser2 object.
     CComQIPtr<IDispatch> spDisp;
     hRes = spBrws->get_Document(&spDisp);
     spDocument = spDisp;

return spDocument;
}

// Converts a IHTMLWindow2 object to a IWebBrowser2. Returns NULL in case of failure.
CComQIPtr<IWebBrowser2> HtmlWindowToHtmlWebBrowser(CComQIPtr<IHTMLWindow2> spWindow)
{
     ATLASSERT(spWindow != NULL);

CComQIPtr<IServiceProvider>  spServiceProvider = spWindow;
     if (spServiceProvider == NULL)
     {
          return CComQIPtr<IWebBrowser2>();
     }

CComQIPtr<IWebBrowser2> spWebBrws;
     HRESULT hRes = spServiceProvider->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&spWebBrws);
     if (hRes != S_OK)
     {
          return CComQIPtr<IWebBrowser2>();
     }

return spWebBrws;
}

附: 
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

ref: 
在多Frame的网页中怎么取出各个Frame的IHTMLDocument2的接口!急用.(高分)
在文章 When IHTMLWindow2::get_document returns E_ACCESSDENIED 解决了iframe 跨域访问的问题

posted on 2008-07-30 19:17 泡泡牛 阅读(5153) 评论(3)  编辑 收藏 引用 所属分类: Develop

获取IE (控件)的所有链接(包括Frameset, iframe)的更多相关文章

  1. WPF获取原始控件样式。

    要获取WPF控件的原始样式,需要我们安装Blend for Visual Studio. 然后,我们打开Blend for Visual Studio,创建一个WPF项目. 然后,我们向页面拖动一个B ...

  2. Selenium IDE安装和检查获取的控件路径技巧

    来源:http://www.jianshu.com/p/0ea2dc83549f 从学习Selenium 开始,都是自己写脚本,后来得知有个插件Selenium IDE可以录制脚本,也懒得用了,觉得自 ...

  3. UGUI 之获取当前控件的高度

    当Canvas Scaler选择Constant Pixel Size 当前的分辨率会被被固定,可以用RectTransform类里面的.rect变量值获取 height或Width. 在次情况下获取 ...

  4. winfrom获取用户控件里的控件对象

    如何获取用户控件里的控件对象呢,其实思路也是很简单的, 比如有一个panel 用户控件 里面有许多的其他控件. 那么要找出一个Label控件怎么找呢,好的.现在我们就开始 首先,一个foreach循环 ...

  5. 获取android控件的高度

    问题 如何获取一个控件的长和高,相信很多朋友第一眼看见这个问题都会觉得很简单,直接在onCreate里面调用getWidth.getMeasuredWidth不就可以获得了吗,但是,事实上是并没有简单 ...

  6. .net获取select控件中的文本内容

    .net获取select控件中的文本内容 2009-11-28 21:19小V古 | 分类:C#/.NET | 浏览1374次 <select id="SecType" st ...

  7. JS获取用户控件中的子控件Id

    用户控件 <asp:HiddenField ID="hfGradeId" runat="server" /> <asp:HiddenField ...

  8. 获取Repeater控件中的每一项数据

    var items = rptList.Items;//获取Repeater控件的所有项 foreach (RepeaterItem item in items)//遍历每一项内容 {   var t ...

  9. JS 获取Button控件的提交类型

    <script type="text/javascript"> <!--获取button控件的类型---> function isAuditOrCancel ...

随机推荐

  1. web 汇率

    http://www.cnblogs.com/beimeng/p/3789940.html 网站虽小,五脏俱全(干货)   前言 最近一个朋友让帮忙做一个汇率换算的网站,用业余时间,到最后总算是实现了 ...

  2. MFC显示bmp图像

    有了bmp文件读写的基础,我们就能够開始用MFC显示BMP图片了. 在这里,事实上微软为我们提供了一个实现bmp文件显示的框架,名叫diblook,我们能够先下载下来看看. 以下上链接:DIBLOOK ...

  3. Linux命令在线中文手册

    很好的Linux学习手册:http://linux.51yip.com/ 来源:http://blog.51yip.com/

  4. PHP导入导出excel表格图片的代码和方法大全

    基本上导出的文件分为两种: 1:类Excel格式,这个其实不是传统意义上的Excel文件,只是因为Excel的兼容能力强,能够正确打开而已.修改这种文件后再保存,通常会提示你是否要转换成Excel文件 ...

  5. 【Cubian】set up

    源: http://mirrors.163.com/.help/debian.html https://lug.ustc.edu.cn/wiki/mirrors/help/debian 下载地址: h ...

  6. ztree获取选中节点时不能进入可视区域出现BUG如何解决

    zTree 是一个依靠 jQuery 实现的多功能 “树插件”.优异的性能.灵活的配置.多种功能的组合是 zTree 最大优点. zTree 的特点编辑 ● zTree v3.0 将核心代码按照功能进 ...

  7. Oracle 10g 数据库手动创建步骤

    Oracle 数据库手动创建步骤 编写初始化参数文件 设置操作系统环境变量 创建实例 以管理员身份连接数据库 启动实例 create database 创建数据库 运行数据字典脚本 Oracle的网络 ...

  8. 第四篇:使用 CUBLAS 库给矩阵运算提速

    前言 编写 CUDA 程序真心不是个简单的事儿,调试也不方便,很费时.那么有没有一些现成的 CUDA 库来调用呢? 答案是有的,如 CUBLAS 就是 CUDA 专门用来解决线性代数运算的库. 本文将 ...

  9. C++随机数生成方法(转载,赶紧搜藏)

    一.C++中不能使用random()函数 =============================================================================== ...

  10. sessionStorage存储json对象

    应用场景: 账单列表中A页面:点击其中的一列,ajax返回的数据在这一页 点击进入账单详情B页面: 因为在A页面已经做过ajax的请求了,所以希望把当前其中的一个数组对象传到B页面中,所以,就考虑到暂 ...