我现在已经实现了用contentWindow获得IFrame中的元素
如果嵌入的iframe页面是普通页面,返回iframe 的 document正常
但是对某些嵌入的iframe页面(一般该iframe页面包含有javascript或者iframe),则无法取得iframe的document,返回iframe 的 document是null
这样我就无法获得iframe内的元素
请高手指教。谢谢

解决方案 »

  1.   

    你是什么时候去访问iframe的document的?检查一下IHTMLFrameBase2::readyState
      

  2.   

    jiangsheng(蒋晟.Net) ,我用vb的调试器查看过,此时IHTMLFrameBase2的readyState的确为"complete"
    也可以获得IHTMLFrameBase2的contentWindow,但是再获取它的document2接口,返回就是NULL
    我查看了这个IHTMLFrameBase2,它里面frame的length为3,再获取其中某个frame的contentWindow也正常,但同样会出现获取document2的错误
      

  3.   

    vc代码如下,请高手指教。谢谢hr = Element->QueryInterface(IID_IHTMLFrameBase2, (void**)&pFrameBase2);
    Element->Release();if (SUCCEEDED(hr) && pFrameBase2)
    {
      hr = pFrameBase2->get_contentWindow(&pWindow2);
      pFrameBase2->Release();  if (SUCCEEDED(hr) && pWindow2)
      {    //hr = pWindow2->get_document(&pDocument2);
        //hr = pWindow2->QueryInterface(IID_IHTMLDocument2, (void**)&pDocument2);
        pWindow2->Release();注释掉的是尝试使用的句子,结果都一样,返回document2都是null
      

  4.   

    换用IWebBrowser2看看
    // Get the IDispatch of the document
    LPDISPATCH lpDisp = NULL;
    lpDisp = m_webBrowser.GetDocument();if (lpDisp)
    {
       IOleContainer* pContainer;   // Get the container
       HRESULT hr = lpDisp->QueryInterface(IID_IOleContainer,
                                           (void**)&pContainer);
       lpDisp->Release();   if (FAILED(hr))
          return hr;   IEnumUnknown* pEnumerator;   // Get an enumerator for the frames
       hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
       pContainer->Release();   if (FAILED(hr))
          return hr;   IUnknown* pUnk;
       ULONG uFetched;   // Enumerate all the frames
       for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++)
       {
          // QI for IWebBrowser here to see if we have an embedded browser
          IWebBrowser2* pBrowser;      hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pBrowser);
          pUnk->Release();      if (SUCCEEDED(hr))
          {
             hr = pBrowser->get_document(&pDocument2);
             ……
             pDocument2->Release();
             pBrowser->Release();
          }
       }   pEnumerator->Release();
    }
      

  5.   

    jiangsheng(蒋晟.Net),我明白你的意思了
    这里的iframe是作为ole容器出现的
    虽然还没试验出结果,但给我很大启发。多谢指教
    呵呵