3,055
社区成员
发帖
与我相关
我的任务
分享
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<nFormCount; 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<nElemCount; 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();
}
}
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();
}