首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从IE中获取最新的IHTMLDocument2对象

如何从IE中获取最新的IHTMLDocument2对象
EN

Stack Overflow用户
提问于 2009-07-13 23:18:32
回答 1查看 3.6K关注 0票数 1

目前,我使用MSAA从IE获取IHTMLDocument2对象。然而,对于一些复杂的web应用程序,这个IHTMLDocument2对象可能包含多个IHTMLDocument2对象,其中一些对象不属于当前显示页面,而是属于前一个页面。

在我看来,IE有时并不重新定义它的DOM对象,而是不断地在DOM中添加更多的IHTMLDocument2对象。我的问题是如何从DOM对象获取当前显示的IHTMLDocument2对象。

提前感谢

更新

嗨雷米

谢谢你的回答。

是的,您是对的,我确实使用框架来访问其他IHTMLDocument2对象。我的理解是,我从HWND获得的IHTMLDocument2对象是它的DOM中的顶级对象。有时,IE也会在其中一个框架中放置IHTMLDocument2对象。

这是我代码的一部分。

代码语言:javascript
复制
BOOL IESpy::GetHTMLText( CComPtr<IHTMLDocument2> spDoc, int tagNo, int schNo)
{
    USES_CONVERSION;

    HRESULT hr = NULL;
    BOOL res = TRUE;
    BOOL doneSearch = FALSE;

    // Extract the source code of the document
    if (spDoc) {
        IHTMLFramesCollection2* pFrames = NULL;
        if (hr = (spDoc->get_frames(&pFrames)) == S_OK){  
            LONG framesCount;
            pFrames->get_length(&framesCount);
            if (framesCount > 0) {
                for( long i=0; i < framesCount; i++) {
                    VARIANT varIdx; 
                    varIdx.vt=VT_I4;
                    VARIANT varResult;
                    varIdx.lVal=i;
                    VariantInit(&varResult);
                    hr = pFrames->item(&varIdx, &varResult);
                    if (SUCCEEDED(hr) && (varResult.vt == VT_DISPATCH)){
                        CComQIPtr<IHTMLWindow2> pFrameWnd;
                        CComQIPtr<IHTMLDocument2> pFrameDoc;
                        CComBSTR description=NULL;
                        pFrameWnd = varResult.pdispVal;
                        VariantClear(&varResult);
                        if (pFrameWnd == 0) {
                            continue;
                        }
                        hr = pFrameWnd->get_document(&pFrameDoc);
                        if (SUCCEEDED(hr) && pFrameDoc){
                            GetHTMLText( pFrameDoc, tagNo, schNo );
                            if ( m_foundText ) {
                                break;
                            }
                        } else if ( hr == E_ACCESSDENIED ) {
                            CComQIPtr<IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser(pFrameWnd);
                            if ( spBrws != NULL) {
                                // Get the document object from the IWebBrowser2 object.
                                CComQIPtr<IDispatch> spDisp;
                                hr = spBrws->get_Document(&spDisp);
                                if ( hr == S_OK ) {
                                    pFrameDoc = spDisp;
                                    if ( pFrameDoc ) {
                                        GetHTMLText( pFrameDoc, tagNo, schNo );
                                        if ( m_foundText ) {
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            pFrames->Release();

            if ( !m_foundText ) {
                res = ReadSearchText(spDoc, tagNo, schNo );
                doneSearch = TRUE;
            }
        }
        if ( !m_foundText && doneSearch == FALSE ) {
            res = ReadSearchText(spDoc, tagNo, schNo );
        }
    }

    return res;
}

BOOL IESpy::ReadSearchText(CComPtr<IHTMLDocument2> spDoc, int tagNo, int schNo )
{
    USES_CONVERSION;

    HRESULT hr = NULL;
    BOOL found = FALSE;

    IHTMLElementCollection *pAll;
    hr = spDoc->get_all(&pAll); 
    if (FAILED(hr))  {
        return FALSE;
    }
    long items;
    IDispatch *ppvDisp;
    IHTMLElement *ppvElement;
    pAll->get_length(&items);

    std::wstring foundText = L"";
    for ( long j = 0; j < items; j++ ) {
        VARIANT index;
        index.vt = VT_I4;
        index.lVal = j;
        hr = pAll->item( index, index, &ppvDisp );
        if (FAILED(hr))  {
            return FALSE;
        }

        if ( ppvDisp ) {
            ppvDisp->QueryInterface(IID_IHTMLElement, (void **)&ppvElement);
            if ( ppvElement ) { 
                CComBSTR bstrTag;
                ppvElement->get_tagName(&bstrTag);
                wchar_t *wtemp = OLE2W(bstrTag);    
                if ( wtemp ) {
                    std::wstring text = ReadSearchText(ppvElement, wtemp, tagNo, schNo, found);
                    if ( !text.empty() ) {
                        if ( !foundText.empty() ) {
                            foundText += concat_string;
                        }
                        foundText += text;
                    }
                    ppvElement->Release();
                    if ( found ) {
                        BOOL stop = FALSE;
                        for ( size_t i = 0; i < m_tagName[tagNo]->size(); i++ ) {
                            if ( wcscmp(m_tagName[tagNo]->at(i).c_str(), L"HTML") == 0 
                                || wcscmp(m_tagName[tagNo]->at(i).c_str(), L"HEAD") == 0 
                                || wcscmp(m_tagName[tagNo]->at(i).c_str(), L"BODY") == 0 ) {
                                stop = TRUE;
                                break;
                            }
                        }
                        if ( stop ) {
                            break;
                        }
                    }
                } else {
                    ppvElement->Release();
                }
            }
        }
    }

    if ( !foundText.empty() ) {
        if ( m_screenCompare ) {
        //  long timeStamp = GetHPTimeStamp(spDoc);
        //  m_temp_results[timeStamp] = foundText;
            m_temp_results.push_back(foundText);
        } else {
            m_result += foundText;
            m_result += L" ";
            m_foundText = TRUE;
        }
    }

    return TRUE;
}
EN

回答 1

Stack Overflow用户

发布于 2009-07-14 00:33:14

IHTMLDocument2不能包含其他IHTMLDocument2对象(除非它们属于页面上的框架),当然也不能包含以前页面中的对象。你是怎么确定这一点的?你能给我看看密码吗?

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1122642

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档