您的位置:首页 > 编程语言 > C语言/C++

遍历当前网页表单,并自动填写提交的问题(VC++)!

2011-11-16 11:55 555 查看
下边的在网上找的代码:
-------------------------------------------------------------------------
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
::CoInitialize(NULL);//初始化 COM 公寓
EnumIE();//枚举浏览器
::CoUninitialize();//释放 COM 公寓
cout << _T("======完成======") << endl;
getchar();//等待回车
return 0;
}
void EnumIE( void )
{
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 的接口指针
EnumForm( spDocument2 );//枚举所有的表单
}
}
void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 )
{
if ( !pIHTMLDocument2 )return;
HRESULT hr;
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
pIHTMLDocument2->get_frames( &spFramesCollection2 );//取得框架frame的集合
long nFrameCount=0;//取得子框架个数
hr = spFramesCollection2->get_length( &nFrameCount );
if ( FAILED ( hr ) || 0 == nFrameCount )return;
for(long i=0; i<nFrameCount; i++)
{
CComVariant vDispWin2;//取得子框架的自动化接口
hr = spFramesCollection2->item( &CComVariant(i), &vDispWin2 );
if ( FAILED ( hr ) )continue;
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 )continue;//取得子框架的 IHTMLWindow2 接口
CComPtr < IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 );//取得字框架的 IHTMLDocument2 接口
EnumForm( spDoc2 );//递归枚举当前子框架 IHTMLDocument2 上的表单form
}
}
void EnumForm( IHTMLDocument2 * pIHTMLDocument2 )
{
if( !pIHTMLDocument2 )return;
EnumFrame( pIHTMLDocument2 );//递归枚举当前 IHTMLDocument2 上的子框架fram
HRESULT hr;
CComBSTR bstrTitle;
pIHTMLDocument2->get_title( &bstrTitle );//取得文档标题
USES_CONVERSION;
cout << _T("====================") << endl;
cout << _T("开始枚举“") << OLE2CT( bstrTitle ) << _T("”的表单") << endl;
cout << _T("====================") << endl;
CComQIPtr< IHTMLElementCollection > spElementCollection;
hr = pIHTMLDocument2->get_forms( &spElementCollection );//取得表单集合
if ( FAILED( hr ) )
{
cout << _T("获取表单的集合 IHTMLElementCollection 错误") << endl;
return;
}
long nFormCount=0;//取得表单数目
hr = spElementCollection->get_length( &nFormCount );
if ( FAILED( hr ) )
{
cout << _T("获取表单数目错误") << endl;
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;
}
//提交这个表单
spFormElement->submit();
}
}
--------------------------------------------------------------------
需要帮助的问题在这里:
表单我也可以遍历出来了,也会提交了,就是在提交前需要有个自动向INPUT中填写信息的功能不会加,请问怎么加哦?谢谢!
也就是执行spFormElement->submit();前应该怎么写,希望可以发上来代码和详细注解!谢谢
这个问题我也在网上找了几天了,人有些笨,不是看不懂,就是不会用,希望大家帮忙(50分)!
goingup 发表于 2006-12-15 14:37:12
gz
coderui 发表于 2006-12-15 16:20:00
怎么实现自动填表呢?
谁帮帮我哦,谢谢!
qiujian5628 发表于 2006-12-15 16:23:20
发送post请求提交表单不行吗?
coderui 发表于 2006-12-15 16:28:08
楼上的朋友你QQ多少啊?喜欢交流吗?我的是460333866
我只是想知道遍历表单后,该怎么去实现自动填表的功能!
谁帮我哦,继续,谢谢!
ppzine 发表于 2006-12-15 18:04:07
这其实就是找到值后,组成一个post的http请求,建立与web服务器的socket 连接,发出去就行了
coderui 发表于 2006-12-15 18:14:39
"这其实就是找到值后,组成一个post的http请求,建立与web服务器的socket 连接,发出去就行了"
有朋友可以告诉我,这个真的就是所谓的"网页自动填表"吗?
谢谢
coderui 发表于 2006-12-16 1:14:52
谁帮帮我啊?我一直在等.
jiangsheng 发表于 2006-12-16 7:28:06
/// <summary>
/// Set the value of the given input element in a form
/// </summary>
/// <param >the name of the input element</param>
/// <param >the new value of the input element</param>
/// <param >the reference of the form element</param>
[CLSCompliant(false), ComVisible(false)]
static public void SetFormInputValue(string inputName, string inputValue, IHTMLFormElement form)
{
if (form== null)
return ;
object o = form.item(inputName, 0);
if (o == null) return;
IHTMLElement element = (IHTMLElement)o;
if (element.tagName.ToLower(System.Globalization.CultureInfo.InvariantCulture).CompareTo("input") == 0)
{
IHTMLInputElement inputElement = (IHTMLInputElement)element;
inputElement.value = inputValue;
}
else if (element.tagName.ToLower(System.Globalization.CultureInfo.InvariantCulture).CompareTo("select") == 0)
{
IHTMLSelectElement selectElement = (IHTMLSelectElement)element;
selectElement.value = inputValue;
}
else if (element.tagName.ToLower(System.Globalization.CultureInfo.InvariantCulture).CompareTo("textarea") == 0)
{
IHTMLTextAreaElement textAreaElement = (IHTMLTextAreaElement)element;
textAreaElement.value = inputValue;
}
}
/// <summary>
chary8088 发表于 2006-12-17 14:47:06
LZ的这个代码我现在就是在用,写信息很简单的:
CString Comstr=lpType,ComVal=lpVal;
CString Tmp="";
Tmp.Format("测试%d智能灌水机器人\nQQ:16645709",j);
if(Comstr.Find("textarea")>=0)//&&ComVal.Find("回复")>=0
{
CComVariant vSetStatus(Tmp);
spInputElement.PutPropertyByName ///这里:)(L"value",&vSetStatus);
Comstr="";
ComVal="";
spFormElement->submit();
}
coderui 发表于 2006-12-17 14:47:06
我朋友说试了,可以填表了.
但是字符串是unsinged short *的形式.
填进去的是乱码,怎么可以填进去CString类型呢?
谢谢!
leo_sysu 发表于 2006-12-17 14:49:43
spInputElement.PutPropertyByName ///这里:)(L"value",&vSetStatus);
好像这个不行,呵呵.
coderui 发表于 2006-12-17 18:04:47
spInputElement.PutPropertyByName ///这里:)(L"value",&vSetStatus);
好像这个不行,呵呵.
谁帮帮忙哦,谢谢!
coderui 发表于 2006-12-19 11:46:15
spInputElement.PutPropertyByName ///这里:)(L"value",&vSetStatus);
谁帮帮哦,谢谢!
chary8088 发表于 2006-12-21 9:00:58
CString Comstr=lpType,ComVal=lpVal;
CString Tmp="";
Tmp.Format("测试%d智能灌水机器人\nQQ:16645709",j);
if(Comstr.Find("textarea")>=0)//&&ComVal.Find("回复")>=0
{
CComVariant vSetStatus(Tmp);
spInputElement.PutPropertyByName(L"value",&vSetStatus);//这样的
Comstr="";
ComVal="";
spFormElement->submit();
}
绝对可以,我天天用
moonthief 发表于 2007-1-5 16:53:41
请问楼上你写过表单自动提交的程序吧。。。能不能给个源码参考一下,我在写一个自动提交内容的程序,我用的是winInet,post的方式,可是经常会卡,不知道怎么解决,如果你的那个方法好的话,想参考一下。。。谢谢 moonthief@163.com
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: