请问各位高手:
问题1、如何通过遍历的方式读取IE浏览器的字段(如input,textarea,..)的值?
我知道的方法是读取HTML源文件的,先讲一下那个程序的过程:先通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口,如pHDoc2,然后采取元件遍历方式:
if(pHDoc2==NULL)return;
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2->get_all(&pAllColl);
if(hr==S_OK){
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr==S_OK){
for(int i=0;i<length;i++){
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pAllColl->item(vName,vIndex,&pDisp);
if( hr==S_OK ){
CComPtr<IHTMLElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if( hr==S_OK ){
CComBSTR tagName;
hr=pElement->get_tagName(&tagName);
if(hr==S_OK){
CString str(tagName);
if(str=="HTML"){
CComBSTR pContent;
hr=pElement->get_outerHTML(&pContent);
if(hr==S_OK){
SaveAndShow(pContent);
i=length;//以便退出循环
}
else{//if get_outerHTML failed
AfxMessageBox("can't get html code");
}
}//else if tagName isnot 'HTML'
}//else if get_tagName failed
}//else if don't get IHMTLElement interface
}//if no items
}
}//if get_length failed
}//if get_all failed
pHDoc2->Release();
这样就读出了html源文件,即采用一个IHTMLElement的集合Collection,然后进行遍历,找到html的TAG,然后读的。
同时我还发现有一个类似的读取鼠标点击框文本显示过程,是采用IHTMLInputTextElement,即先找到鼠标点击处的Element,然后用:
CComPtr<IHTMLInputTextElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&pElement);
然后用get_value读取文本的。这个程序只能单独取一个文本框的值。
而我的意图是不用鼠标点击文本框,而是在网页提交的时候,VC程序自动读取网页内的所有文本框内的值并记录下来,所以必须解决网页InputTextElement的遍历问题。我试图找到一个IHTMLInputTextElement的集合Collection,但是没有,那样我如何通过遍历的方式找到网页中的文本框并取得值呢?讲了这大半天,也不知道讲明白没有,如果不清楚,请告诉我,我马上补充,谢谢。问题2、网页提交有几种办法,(我不是指get 和submit,呵呵),我是指回车提交和用鼠标点提交按钮两种可能的操作方式,这样会产生许多消息,典型消息是什么呢?也就是说,我如何才能从众多的消息只识别出只有网页提交才会出现的消息呢?消息中有句柄的内容么?以上回答任一问题均有分,谢谢!
问题1、如何通过遍历的方式读取IE浏览器的字段(如input,textarea,..)的值?
我知道的方法是读取HTML源文件的,先讲一下那个程序的过程:先通过WM_HTML_GETOBJECT取得控件的IHTMLDocument2接口,如pHDoc2,然后采取元件遍历方式:
if(pHDoc2==NULL)return;
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2->get_all(&pAllColl);
if(hr==S_OK){
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr==S_OK){
for(int i=0;i<length;i++){
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pAllColl->item(vName,vIndex,&pDisp);
if( hr==S_OK ){
CComPtr<IHTMLElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if( hr==S_OK ){
CComBSTR tagName;
hr=pElement->get_tagName(&tagName);
if(hr==S_OK){
CString str(tagName);
if(str=="HTML"){
CComBSTR pContent;
hr=pElement->get_outerHTML(&pContent);
if(hr==S_OK){
SaveAndShow(pContent);
i=length;//以便退出循环
}
else{//if get_outerHTML failed
AfxMessageBox("can't get html code");
}
}//else if tagName isnot 'HTML'
}//else if get_tagName failed
}//else if don't get IHMTLElement interface
}//if no items
}
}//if get_length failed
}//if get_all failed
pHDoc2->Release();
这样就读出了html源文件,即采用一个IHTMLElement的集合Collection,然后进行遍历,找到html的TAG,然后读的。
同时我还发现有一个类似的读取鼠标点击框文本显示过程,是采用IHTMLInputTextElement,即先找到鼠标点击处的Element,然后用:
CComPtr<IHTMLInputTextElement> pElement;
hr=pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&pElement);
然后用get_value读取文本的。这个程序只能单独取一个文本框的值。
而我的意图是不用鼠标点击文本框,而是在网页提交的时候,VC程序自动读取网页内的所有文本框内的值并记录下来,所以必须解决网页InputTextElement的遍历问题。我试图找到一个IHTMLInputTextElement的集合Collection,但是没有,那样我如何通过遍历的方式找到网页中的文本框并取得值呢?讲了这大半天,也不知道讲明白没有,如果不清楚,请告诉我,我马上补充,谢谢。问题2、网页提交有几种办法,(我不是指get 和submit,呵呵),我是指回车提交和用鼠标点提交按钮两种可能的操作方式,这样会产生许多消息,典型消息是什么呢?也就是说,我如何才能从众多的消息只识别出只有网页提交才会出现的消息呢?消息中有句柄的内容么?以上回答任一问题均有分,谢谢!
MSHTML::IHTMLInputElementPtr pInput(disp);
if(pInput)
{
BSTR bstrtype;
pInput->get_type(&bstrtype);
printf(_bstr_t(bstrtype));
if(StrCmpW(bstrtype,L"password")==0)
{
BSTR bstrval;
pInput->get_value(&bstrval);
bret = true;
wsprintf(buf,"%s",(char*)_bstr_t(bstrval));
SysFreeString(bstrval);
}
SysFreeString(bstrtype);
}
现在请朋友们帮我解决第二个问题!即:网页提交有几种办法,(我不是指get和post),我是指回车提交和用鼠标点提交按钮两种可能的操作方式,这样会产生许多消息,典型消息是什么呢?也就是说,我如何才能从众多的消息只识别出只有网页提交才会出现的消息呢?消息中有句柄的内容么?
static FILE *stream;
CComBSTR Title,Tag;
CComBSTR Name,Type,Value;
CString strName,strType,strValue,strTag,strTitle;
if(pHDoc2==NULL)return;
CComPtr<IHTMLElementCollection> pAllColl;
HRESULT hr;
hr=pHDoc2->get_all(&pAllColl);
if(hr==S_OK){
LONG length=0;
hr=pAllColl->get_length(&length);
if(hr==S_OK){
stream = fopen("c:\\sxh.tmp", "a+");
for(int i=0;i<length;i++){
VARIANT vIndex,vName;
vName.vt=vIndex.vt=VT_I4;
vName.lVal=vIndex.lVal=i;
CComPtr<IDispatch> pDisp;
hr=pAllColl->item(vName,vIndex,&pDisp);
if( hr==S_OK ){
CComPtr<IHTMLInputTextElement> pElement_Input;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLTextAreaElement> pElement_TextArea;
CComPtr<IHTMLSelectElement> pElement_Select;
CComBSTR Title;
hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
if( hr==S_OK ){
hr=pElement->get_tagName(&Tag);
if(hr==S_OK)
{
strTag = (CString)(Tag);
if (strTag == "TITLE")
{
hr=pElement->get_outerText(&Title);
strTitle = (CString)(Title);
fprintf(stream,"网页名:%s\r\n",strTitle);
continue;
}
}
}
hr=pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&pElement_Input); //强试,类似可以用textarea,select,是该类型才做下一步
if( hr==S_OK ){
hr=pElement_Input->get_name(&Name);
if(hr==S_OK)
{
strName = (CString)(Name);
}
else
return ;
hr = pElement_Input->get_type(&Type);
if(hr==S_OK)
{
strType = (CString)(Type);
}
else
return;
hr = pElement_Input->get_value(&Value);
if(hr==S_OK)
{
strValue = (CString)(Value);
}
else
return;
// s3 = pElement->Value;
fprintf(stream, "字段名:%s 类型:%s 内容:%s \r\n", strName,strType,strValue);
}// if(hr==S_OK){
//
// CString str(tagName);
// if(str=="HTML"){
// CComBSTR pContent;
// hr=pElement->get_outerHTML(&pContent);
// }//else if tagName isnot 'HTML'
// }//else if get_tagName failed
}//else if don't get IHMTLElement interface
}//if no items
fclose (stream);
}//if get_length failed
}//if get_all failed
pHDoc2->Release();第二个问题解决思路是:
1、不能截取wm_killfocus,wm_setfocus,capturechanged等。只能截取wm_parentnotify中细节消息为wm_destroy的消息。我正在试,如果成功,再告诉大家!
另外,还有一种更好的办法,就是截获网页的提交event,关键是不知道如何截获,不知哪位大虾能提示点有用的信息?
但我想详细搞清IE窗口的机制,所以跟jiang Sir 学习一下,还请不吝赐教。如果您知道有关详细讲解的地方,也请告诉我! 谢谢!
我写过的一个论坛灌水机采用的是笨办法。
先读取HTML页面成为TXT文件
然后自己对TXT文件进行分析,
得到提交的CGI请求
Reusing the WebBrowser Control: Working with WebBrowser Events
http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/webbrowser.asp
wellcomezhu(加莱)
fengzi_zhu(疯子)三位朋友,因为你们一直没到,我先结帖,但你们的帮助我会记得,请到我的任何一个未结帖主题里报个号,我会给你们也加分的。再次对所有帮忙的朋友表示感谢。