在IE DOM C ++中引用iframe

时间:2013-07-16 12:28:42

标签: mshtml

我一直致力于一个自动化Internet Explorer的项目。 我需要使用没有.net / ATL / Afx或其他任何东西的win32 C ++。

我对COM和接口非常不熟悉,但已经设法将一个工作函数(借助各个地方的示例)拼凑起来让我开始。

该功能将在网页中找到一个元素并将其滚动到页面顶部(scrollintoview)

但是,我很快意识到某些元素可能在iframe中,我无法弄清楚如何使用iframe获取对元素/文档的引用。

如果有人能帮助我,我将不胜感激。

这是我现在的功能。

    void _scroll(){

    BSTR targetElementID = L"p_13872472-header-image";

    IHTMLDocument2 *Document;
    IDispatch *Dispatch;
    IHTMLElement *Element;
    HRESULT      Result;

    Result = NULL;
    Result = pBrowser->get_Document(&Dispatch);
    if (!SUCCEEDED(Result) || Dispatch == NULL){
        _MessageBox("Error", "pBrowser->get_Document(&Dispatch)");
        return;
    }

    Result = NULL;
    Result = Dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&Document);
    if (FAILED(Result) || Document == NULL){
        _MessageBox("Error", "Dispatch->QueryInterface(IID_IHTMLDocument2");
        return;
    }
    Dispatch->Release();

    IHTMLElement *lpBodyElm;
    IHTMLElement *lpParentElm;

    Result = NULL;
    Result = Document->get_body(&lpBodyElm);
    if (!SUCCEEDED(Result) || lpBodyElm == NULL){
        _MessageBox("Error", "Document->get_body(&lpBodyElm)");
        Document->Release();
        return;
    }

    IHTMLElementCollection* pElemColl;
    IDispatch* pElemDisp = NULL;

    Result = NULL;
    Result = Document->get_all(&pElemColl);
    if (!SUCCEEDED(Result)) {
        _MessageBox("Error", "Result = Document->get_all(&pElemColl)");
        return;
    }

    IHTMLElement* pElem = NULL;
    _variant_t varName(targetElementID);
    _variant_t varIdx(0L);

    Result = NULL;
    Result = pElemColl->item(varName, varIdx, &pElemDisp);
    if (!SUCCEEDED(Result)) {
        _MessageBox("Error", "pElemColl->item(varName, varIdx, &pElemDisp)");
        Document->Release();
        return;
    }

    Result = NULL;
    Result = pElemDisp->QueryInterface(IID_IHTMLElement, (void**)&pElem);
    if (!SUCCEEDED(Result)) {
        _MessageBox("Error", "pElemDisp->QueryInterface(IID_IHTMLDOMNode, (void**)&pElem)");
        Document->Release();
        return;
    }
       _variant_t mybool = -1;
    pElem->scrollIntoView(mybool);
      _MessageBox("woohoo", "Good so far");
}

0 个答案:

没有答案
相关问题