簡體   English   中英

如何從 IE DOM 獲取最新的 IHTMLDocument2 對象

[英]How to get the most current IHTMLDocument2 object from IE DOM

目前,我使用 MSAA 從 IE HWND 獲取 IHTMLDocument2 對象。 但是,對於一些復雜的Web應用程序,這個IHTMLDocument2對象可能包含多個IHTMLDocument2對象,其中一些不屬於當前顯示頁面,而是屬於上一個頁面。

在我看來,IE 有時不會刷新其 DOM 對象,而是不斷將更多 IHTMLDocument2 對象添加到其 DOM 中。 我的問題是如何從 DOM 對象中獲取當前顯示的 IHTMLDocument2 對象。

提前致謝

更新

嗨雷米,

感謝您的回答。

是的,你是對的,我確實使用框架來訪問其他 IHTMLDocument2 對象。 我的理解是,我從 HWND 獲得的 IHTMLDocument2 對象是其 DOM 中的頂級對象。 IE 有時也會將先前的 IHTMLDocument2 對象放入其中一個框架中。

這是我的代碼的一部分。

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;
}

IHTMLDocument2 不能包含其他 IHTMLDocument2 對象(除非它們屬於頁面上的框架),當然也不能包含來自以前的頁面。 你是如何確定的? 你能展示一些代碼嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM