kevin55

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
void CAttendanceRobotDlg::DocumentCompleteExplorer4(LPDISPATCH pDisp, VARIANT* URL)
{
    // TODO: Add your message handler code here
    HRESULT   hr;
    LPUNKNOWN lpUnknown;
    LPUNKNOWN lpUnknownWB = NULL;
    LPUNKNOWN lpUnknownDC = NULL;
    IHTMLElementCollection *objAllElement = NULL;
    IHTMLDocument2 *objDocument = NULL;
    CString strUrl, strTemp;

    lpUnknown = m_pBrowser->GetControlUnknown();
    ASSERT(lpUnknown);

    if (lpUnknown)
    {
        hr = lpUnknown->QueryInterface(IID_IUnknown, (LPVOID*)&lpUnknownWB);
        ASSERT(SUCCEEDED(hr));
        if (FAILED(hr))
        {
            LOGD(L"QueryInterface IID_IUnknown failed.");
            return;
        }

        hr = pDisp->QueryInterface(IID_IUnknown, (LPVOID*)&lpUnknownDC);
        ASSERT(SUCCEEDED(hr));
        if (SUCCEEDED(hr) && lpUnknownWB == lpUnknownDC)
        {
            // The document has finished loading.
            strUrl = m_pBrowser->get_LocationURL();
            if (strUrl.IsEmpty())
            {
                LOGD(L"get_LocationURL strUrl.IsEmpty().");
                return;
            }
            objDocument = (IHTMLDocument2*)m_pBrowser->get_Document();
            objDocument->get_all(&objAllElement);
            if (strUrl == _T("http://www.baidu.com/"))
            {
                CComPtr<IDispatch>pDisp;
                objAllElement->item(COleVariant(_T("kw")), COleVariant((long)0), &pDisp);
                CComQIPtr<IHTMLElement, &IID_IHTMLElement>pElement;
                if (pDisp == NULL)
                {
                    LOGD(L"pDisp == NULL.");
                    return;
                }
                else
                {
                    pElement = pDisp;
                    pElement->put_innerText(_T("Hello")); //填充表单
                }

                VARIANT   name;
                CComBSTR   tag;
                long     index;
                objAllElement->get_length(&index);
                name.vt = VT_I4;
                for (long i = 0; i < index; i++)//遍历所有元素,通过id属性获取值
                {
                    name.lVal = i;
                    IDispatch   *   pDispatch = NULL;
                    objAllElement->item(name, name, &pDispatch);
                    IHTMLElement*   spElement;
                    pDispatch->QueryInterface(IID_IHTMLElement, (void**)&spElement);
                    BSTR   tag;
                    spElement->get_id(&tag);
                    CString   ss(tag);
                    if (ss == "su")
                    {
                        spElement->click();
                    }

                    spElement->Release();
                }
            }
        }
    }

    if (lpUnknownWB)
    {
        lpUnknownWB->Release();
    }

    if (lpUnknownDC)
    {
        lpUnknownDC->Release();
    }
}

posted on 2015-08-24 19:45  kernel_main  阅读(1509)  评论(0编辑  收藏  举报