添加填充表单函数填充用户名和密码
[cpp] view plaincopyprint?
void CAutoLoginView::AutoFillForm(IHTMLDocument2 *pIHTMLDocument2, CComVariant username, CComVariant password)
{
if( !pIHTMLDocument2 )return;
HRESULT hr;
CComBSTR bstrTitle;
pIHTMLDocument2->get_title( &bstrTitle );//取得文档标题
CComQIPtr< IHTMLElementCollection > spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );//取得表单集合
if ( FAILED( hr ) )
{
AfxMessageBox("获取表单的集合 IHTMLElementCollection 错误");
return;
}
long nFormCount=0;//取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
AfxMessageBox("获取表单数目错误");
return;
}
for(long i=0; i//遍历表单
{
IDispatch *pDisp = NULL;//取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) )continue;
CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();
long nElemCount=0;//取得表单中 域 的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) )continue;
for(long j=0; j
{
CComDispatchDriver spInputElement;//取得第 j 项表单域
CComVariant vName,vVal,vType;//取得表单域的名,值,类型
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) )continue;
hr = spInputElement.GetPropertyByName(L"name", &vName);
if(vName == (CComVariant)"email")
{
vVal = username;
spInputElement.PutPropertyByName(L"value",&vVal);
}
if(vName == (CComVariant)"password")
{
vVal = password;
spInputElement.PutPropertyByName(L"value",&vVal);
}
}
//提交表单
//spFormElement->submit();
}
}
5、模拟登录按钮单击
[cpp] view plaincopyprint?
void CAutoLoginView::AutoLogin()
{
IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;
objDocument=(IHTMLDocument2 *)GetHtmlDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合
IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);
name.vt=VT_I4;;
for(int i=0;i//遍历所有元素
{
name.lVal = i;
IDispatch * pDispatch=NULL;
HRESULT res = objAllElement->item(name,name,&pDispatch);
if (FAILED(res))
{
continue;
}
IHTMLInputButtonElement *spInputText;
HRESULT rsc = pDispatch->QueryInterface(IID_IHTMLInputButtonElement, (void**)&spInputText);
if (FAILED(rsc))
{
continue;
}
BSTR bstrType;
spInputText->get_type(&bstrType);
CString strType(bstrType);
if (strType.CompareNoCase("submit") == 0)
{
BSTR bstrVal;
spInputText->get_value(&bstrVal);
CString strVal(bstrVal);
if (strVal.CompareNoCase("登录人人网") == 0)
{
VARIANT vardisp;
vardisp.vt=VT_DISPATCH;
vardisp.pdispVal=spInputText;
IHTMLElement* pElem = NULL;
spInputText->QueryInterface(IID_IHTMLElement, (void**)&pElem);
pElem->click();
HRESULT hr = objDocument->put_onclick(vardisp);
if (FAILED(hr))
{
pElem->click();
continue;
}
break;
}
}
}
}
6、添加工具图标相应
[cpp] view plaincopyprint?
void CAutoLoginView::OnLoginRenren()
{
// TODO: Add your command handler code here
Navigate2("http://www.renren.com/",NULL,NULL);
IHTMLDocument2* pHtmlDoc2=(IHTMLDocument2*)GetHtmlDocument();
CComVariant email =L"123@qq.com"; // construct from a LPCSTR 账号
CComVariant password=L"123456"; //密码
AutoFillForm(pHtmlDoc2,email,password);
AutoLogin();
}
[cpp] view plaincopyprint?
void CAutoLoginView::AutoFillForm(IHTMLDocument2 *pIHTMLDocument2, CComVariant username, CComVariant password)
{
if( !pIHTMLDocument2 )return;
HRESULT hr;
CComBSTR bstrTitle;
pIHTMLDocument2->get_title( &bstrTitle );//取得文档标题
CComQIPtr< IHTMLElementCollection > spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );//取得表单集合
if ( FAILED( hr ) )
{
AfxMessageBox("获取表单的集合 IHTMLElementCollection 错误");
return;
}
long nFormCount=0;//取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
AfxMessageBox("获取表单数目错误");
return;
}
for(long i=0; i//遍历表单
{
IDispatch *pDisp = NULL;//取得第 i 项表单
hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp );
if ( FAILED( hr ) )continue;
CComQIPtr< IHTMLFormElement > spFormElement = pDisp;
pDisp->Release();
long nElemCount=0;//取得表单中 域 的数目
hr = spFormElement->get_length( &nElemCount );
if ( FAILED( hr ) )continue;
for(long j=0; j
{
CComDispatchDriver spInputElement;//取得第 j 项表单域
CComVariant vName,vVal,vType;//取得表单域的名,值,类型
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) )continue;
hr = spInputElement.GetPropertyByName(L"name", &vName);
if(vName == (CComVariant)"email")
{
vVal = username;
spInputElement.PutPropertyByName(L"value",&vVal);
}
if(vName == (CComVariant)"password")
{
vVal = password;
spInputElement.PutPropertyByName(L"value",&vVal);
}
}
//提交表单
//spFormElement->submit();
}
}
5、模拟登录按钮单击
[cpp] view plaincopyprint?
void CAutoLoginView::AutoLogin()
{
IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;
objDocument=(IHTMLDocument2 *)GetHtmlDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合
IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);
name.vt=VT_I4;;
for(int i=0;i//遍历所有元素
{
name.lVal = i;
IDispatch * pDispatch=NULL;
HRESULT res = objAllElement->item(name,name,&pDispatch);
if (FAILED(res))
{
continue;
}
IHTMLInputButtonElement *spInputText;
HRESULT rsc = pDispatch->QueryInterface(IID_IHTMLInputButtonElement, (void**)&spInputText);
if (FAILED(rsc))
{
continue;
}
BSTR bstrType;
spInputText->get_type(&bstrType);
CString strType(bstrType);
if (strType.CompareNoCase("submit") == 0)
{
BSTR bstrVal;
spInputText->get_value(&bstrVal);
CString strVal(bstrVal);
if (strVal.CompareNoCase("登录人人网") == 0)
{
VARIANT vardisp;
vardisp.vt=VT_DISPATCH;
vardisp.pdispVal=spInputText;
IHTMLElement* pElem = NULL;
spInputText->QueryInterface(IID_IHTMLElement, (void**)&pElem);
pElem->click();
HRESULT hr = objDocument->put_onclick(vardisp);
if (FAILED(hr))
{
pElem->click();
continue;
}
break;
}
}
}
}
6、添加工具图标相应
[cpp] view plaincopyprint?
void CAutoLoginView::OnLoginRenren()
{
// TODO: Add your command handler code here
Navigate2("http://www.renren.com/",NULL,NULL);
IHTMLDocument2* pHtmlDoc2=(IHTMLDocument2*)GetHtmlDocument();
CComVariant email =L"123@qq.com"; // construct from a LPCSTR 账号
CComVariant password=L"123456"; //密码
AutoFillForm(pHtmlDoc2,email,password);
AutoLogin();
}