首先写一个函数
BOOL CPost::FindSpecifiedElement(CString &csFormName, //元素所在表单名称
CString &csElementTag, //元素标记名称
CString &csElementName //元素名称
)
{
if(csFormName.IsEmpty()||csElementTag.IsEmpty()||
csElementName.IsEmpty())
return false;
bool retVal=false;
IDispatch* pDisp=NULL;
IHTMLDocument2* pHTMLDocument2=NULL;
HRESULT hr=-1;
pDisp = m_browser.GetDocument();
if (pDisp == NULL )
{
TRACE("can not fetch document interface/n");
return retVal;
}
hr = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pHTMLDocument2 );
if (pHTMLDocument2==NULL) // fetch IHTMLDocument2 infterface failed
{
TRACE("can not fetch document interface/n");
pDisp->Release();
return retVal;
}
EnumForm( pHTMLDocument2 );
return retVal;
}
void CPost::EnumForm( IHTMLDocument2 * pIHTMLDocument2 )
{
CString StrInfo;
StrInfo = "";
if( !pIHTMLDocument2 ) return;
HRESULT hr;
CComBSTR bstrTitle;
pIHTMLDocument2->get_title( &bstrTitle );
if (bstrTitle=="Ask361")
{
//StrInfo += (CString)bstrTitle;
//StrInfo +="/r/n";
USES_CONVERSION;
CComQIPtr< IHTMLElementCollection > spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection ); //取得表单集合
if ( FAILED( hr ) )
{
return;
}
long nFormCount=0; //取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
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 项表单域
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"); //未知类型
//cout << _T("[") << lpType << _T("] ");
//cout << lpName << _T(" = ") << lpVal << endl;
//AfxMessageBox((CString)lpType);
//AfxMessageBox((CString)lpVal);
//StrInfo += (CString)lpType;
//StrInfo +="/r/n";
//StrInfo += (CString)lpVal;
//StrInfo +="/r/n";
}
//想提交这个表单吗?删除下面语句的注释吧
//pForm->submit();
}
//AfxMessageBox(StrInfo);
}
}
void CPost::EnumIE()
{
//cout << _T("开始扫描系统中正在运行的浏览器实例") << endl;
CComPtr< IShellWindows > spShellWin;
HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
if ( FAILED ( hr ) )
{
//cout << _T("获取 IShellWindows 接口错误") << endl;
return;
}
long nCount = 0; // 取得浏览器实例个数(Explorer 和 IExplorer)
spShellWin->get_Count( &nCount );
if( 0 == nCount )
{
//cout << _T("没有在运行着的浏览器") << endl;
return;
}
for(int i=0; i<nCount; i++)
{
CComPtr< IDispatch > spDispIE;
hr=spShellWin->Item(CComVariant( (long)i ), &spDispIE );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IWebBrowser2 > spBrowser = spDispIE;
if ( !spBrowser ) continue;
CComPtr < IDispatch > spDispDoc;
hr = spBrowser->get_Document( &spDispDoc );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc;
if ( !spDocument2 ) continue;
// 程序运行到此,已经找到了 IHTMLDocument2 的接口指针
// 删除下行语句的注释,把浏览器的背景改变看看
// spDocument2->put_bgColor( CComVariant( "green" ) );
EnumForm( spDocument2 ); //枚举所有的表单
}
}
然后这样调用:
CString FormName,ElementTarget,ElementName;
FormName= "form1";
ElementTarget = "input";
ElementName = "TitleTest";
if(!FindSpecifiedElement(FormName,ElementTarget,ElementName))
AfxMessageBox("在当前页面无法找到指定元素");