16,471
社区成员
发帖
与我相关
我的任务
分享
下面的代码想实现:把移动宽带连接的Web网站加载到WebBrowse控件中,
自动输入用户名和密码,然后自动提交表单。
现在可以实现加载页面,并向用户名和密码文本框中填充数据,
但是提交时没反应,好象F5刷新页面一样,
也没跳转到提示用户名和密码错误的页面。
请大家帮忙看下原因,谢谢。。。
// WebTestDlg.h : header file
class CWebTestDlg : public CDialog
{
CWebBrowser2 m_ctrlWeb;
};
// WebTestDlg.cpp : implementation file
//引用HTML相关头文件
#include <atlbase.h>
CComModule _Module;
#include <mshtml.h>
#include <atlcom.h>
#include <string>
using namespace std;
//声明自动提交表单的函数
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2);
BOOL CWebTestDlg::OnInitDialog()
{
//默认打开移动宽带网站
m_ctrlWeb.Navigate("http://221.178.143.198:7001/style/default/default.jsp",NULL,NULL,NULL,NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
//实现自动提交表单
void PutFormValue(IHTMLDocument2 * pIHTMLDocument2)
{
if(!pIHTMLDocument2)
return;
HRESULT hr;
CComBSTR bstrTitle;
//获取加载页面的标题
pIHTMLDocument2->get_title( &bstrTitle );
USES_CONVERSION;
CComQIPtr<IHTMLElementCollection>spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );
if (FAILED(hr))
{
AfxTrace(_T("获取表单的集合 IHTMLElementCollection 错误"));
return;
}
long nFormCount=0;
//获取表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
AfxTrace( _T("获取表单数目错误"));
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;
CString strName;
CString strVal;
for(long j=0; j<nElemCount; j++)
{
CComDispatchDriver spInputElement; //取得第 j 项表单域
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement );
if ( FAILED( hr ) ) continue;
CComVariant vName,vVal,vType; //取得表单域的 名,值,类型
hr = spInputElement.GetPropertyByName( L"name", &vName );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"value", &vVal );
if( FAILED( hr ) ) continue;
hr = spInputElement.GetPropertyByName( L"type", &vType );
if( FAILED( hr ) ) continue;
LPCTSTR lpName = vName.bstrVal?
OLE2CT( vName.bstrVal ) : _T("NULL"); //未知域名
LPCTSTR lpVal = vVal.bstrVal?
OLE2CT( vVal.bstrVal ) : _T("NULL"); //空值,未输入
LPCTSTR lpType = vType.bstrVal?
OLE2CT( vType.bstrVal ) : _T("NULL"); //未知类型
strName = lpName;
strName.TrimLeft();
strName.TrimRight();
//向用户名文本框内填充数据
if (strName == "UserName")
{
TCHAR szText[32] = "123456";
CComVariant vMyVal = (LPCTSTR)(szText);
spInputElement.PutPropertyByName( L"value",&vMyVal);
}
//向密码文本框内填充数据
if (strName == "PassWord")
{
TCHAR szText[32] = "789";
CComVariant vMyVal = (LPCTSTR)(szText);
spInputElement.PutPropertyByName( L"value",&vMyVal);
}
} //for(long j=0; j<nElemCount; j++)
//提交表单,感觉象是按了F5刷新
//页面没跳转到提示用户名和密码错误的页面
spFormElement->submit();
} //for(long i=0; i<nFormCount; i++)
}
//测试自动提交表单
void CWebTestDlg::OnButton1()
{
//创建IHTMLDocument2类型的对象
CComPtr <IDispatch> spDispDoc;
spDispDoc = m_ctrlWeb.GetDocument();
CComQIPtr<IHTMLDocument2> spDocument2 = spDispDoc;
if (!spDocument2)
return;
PutFormValue(spDocument2);
}
AfxMessageBox("准备提交");
spFormElement->submit();
AfxMessageBox("提交结束");