谢谢

解决方案 »

  1.   

    void CAutoCheckIDView::Login()
    {
    IDispatch* pDisp;
    IHTMLDocument2* pHTMLDocument2;
    HRESULT hr;
    VARIANT id, index;

    CString mStr; BSTR bsUserID = m_strUserID.AllocSysString();
    BSTR bsPwd = m_strPwd.AllocSysString();

    pDisp = GetHtmlDocument();
    hr = pDisp->QueryInterface( IID_IHTMLDocument2,
    (void**)&pHTMLDocument2 );//Ask for an HTMLDocument2 interface
    IHTMLElementCollection* pColl = NULL;//Enumerate the HTML elements

    hr = pHTMLDocument2->get_all( &pColl );
    if (hr != S_OK || pColl == NULL)
    {
    pHTMLDocument2->Release();
    CoUninitialize();
    return;
    }
    LONG celem;
    hr = pColl->get_length( &celem );//Find the count of the elements

    if ( hr != S_OK )
    {
    pHTMLDocument2->Release();
    pColl->Release();
    CoUninitialize();
    return;
    }
    for ( int i=0; i< celem; i++ )//Loop through each elment
    {
    IDispatch* pDisp2;

    V_VT(&id) = VT_I4;
    V_I4(&id) = i;
    V_VT(&index) = VT_I4;
    V_I4(&index) = 0;
    hr = pColl->item( id,index, &pDisp2 );//Get an element

    if ( hr == S_OK )
    {
    IHTMLElement* pElem;

    //Ask for an HTMLElemnt interface
    hr = pDisp2->QueryInterface(IID_IHTMLElement,(void **)&pElem);
    if ( hr == S_OK )
    {
    BSTR bstr;

    IHTMLInputTextElement* pUser;//We need to check for input elemnt on login screen
    hr = pDisp2->QueryInterface(IID_IHTMLInputTextElement,(void **)&pUser );
    if ( hr == S_OK )
    {
    pUser->get_name(&bstr);
    mStr=bstr;
    if(mStr=="email")
    {//Is this a User Id frield
    pUser->put_value(bsUserID);//Paste the User Id
    }
    else if(mStr=="passwd")
    {//Or, is this a password field
    pUser->put_value(bsPwd);// Paste your password into the field                 
    }
    pUser->Release();
    }
    else
    {
    IHTMLInputImage *pImage = NULL;
    pElem->QueryInterface(&pImage);
    //hr = pDisp->QueryInterface(IID_IHTMLInputImage,(void **)&pImage);
    if( pImage != NULL )
    {
    BSTR bValue;
    pImage->get_src(&bValue);
    CString src(bValue);
    SysFreeString(bValue);

    if(src.Find("btn_login_1.gif") != -1 )
    {
    pElem->click();
    }
    pImage->Release();
    }
    }
    pElem->Release();
    }
    pDisp2->Release();
    }
    }

    pColl->Release();

    pHTMLDocument2->Release();//For the next page open a fresh document
    pDisp->Release();
    }