vc遍历网页表单并自动填写提交
1.獨立代碼
//-----------開始---------------------//
#include <atlbase.h>
#include <Mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//處理網(wǎng)頁
void EnumFrame(IHTMLDocument2 * pIHTMLDocument2);//處理框架
void EnumForm(IHTMLDocument2 * pIHTMLDocument2);//處理表單
CComModule _Module;??//使用CComDispatchDriver ATL的智能指針,此處必須聲明
#include <atlcom.h>
void EnumField(CComDispatchDriver spInputElement,CString ComType,CString ComVal,CString ComName);//處理表單域
void EnumIE(void)??
{??
? CComPtr<IShellWindows> spShellWin;??
? HRESULT hr=spShellWin.CoCreateInstance(CLSID_ShellWindows);??
? if (FAILED(hr))??
? {??
? return;??
? }??????
? long nCount=0;??? //取得瀏覽器實例個數(shù)(Explorer和IExplorer)??
? spShellWin->get_Count(&nCount);??
? if (0==nCount)??
? {??
??? 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;??????
?//Modify by jncao 2007-09-17
?//*******************************************************************************
?CString cIEUrl_Filter;? //設(shè)置的URL(必須是此URL的網(wǎng)站才有效);
??? cIEUrl_Filter="http://127.0.0.1/SmtCCS_manage/"; //設(shè)置過濾的網(wǎng)址
??? //*******************************************************************************
??? CComBSTR IEUrl;
?spBrowser->get_LocationURL(&IEUrl);???
?CString cIEUrl_Get;???? //從機器上取得的HTTP的完整的URL;
?cIEUrl_Get=IEUrl;
?cIEUrl_Get=cIEUrl_Get.Left(cIEUrl_Filter.GetLength()); //截取前面N位
?if (strcmp(cIEUrl_Get,cIEUrl_Filter)==0)
?{
??? ?// 程序運行到此,已經(jīng)找到了IHTMLDocument2的接口指針??????
??????? EnumForm(spDocument2); //枚舉所有的表單????????
?}???
???
? }??
}
void EnumFrame(IHTMLDocument2 * pIHTMLDocument2)
{??
?if (!pIHTMLDocument2) return;??????
?HRESULT?? hr;??
???
?CComPtr<IHTMLFramesCollection2> spFramesCollection2;??
?pIHTMLDocument2->get_frames(&spFramesCollection2); //取得框架frame的集合??
???
?long nFrameCount=0;??????? //取得子框架個數(shù)??
?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);????? //遞歸枚舉當(dāng)前子框架?? IHTMLDocument2?? 上的表單form??
?}??
}
void EnumForm(IHTMLDocument2 * pIHTMLDocument2)??
{??
? if (!pIHTMLDocument2) return;
?
? EnumFrame(pIHTMLDocument2);?? //遞歸枚舉當(dāng)前IHTMLDocument2上的子框架frame??
? HRESULT hr;
?????
? USES_CONVERSION;??????
? CComQIPtr<IHTMLElementCollection> spElementCollection;??
? hr=pIHTMLDocument2->get_forms(&spElementCollection); //取得表單集合??
? if (FAILED(hr))??
? {??
??? return;??
? }??
???
? long nFormCount=0;?????????? //取得表單數(shù)目??
? 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;???????? //取得表單中域的數(shù)目??
??? 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;???? //取得表單域的名稱,數(shù)值,類型
????? 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"); //未知類型?
??
?? EnumField(spInputElement,lpType,lpVal,lpName);//傳遞并處理表單域的類型、值、名
?}//表單域循環(huán)結(jié)束????
? }//表單循環(huán)結(jié)束??????
}??
void EnumField(CComDispatchDriver spInputElement,CString ComType,CString ComVal,CString ComName)
{//處理表單域
?if ((ComType.Find("text")>=0) && strcmp(ComVal,"NULL")==0 && strcmp(ComName,"userName")==0)
?? {
??? ?CString Tmp="123456";
???? CComVariant vSetStatus(Tmp);
??spInputElement.PutPropertyByName(L"value",&vSetStatus);
?? }
?? if ((ComType.Find("password")>=0) && strcmp(ComVal,"NULL")==0 && strcmp(ComName,"password")==0)
?? {
??? ?CString Tmp="123456";
???? CComVariant vSetStatus(Tmp);
??spInputElement.PutPropertyByName(L"value",&vSetStatus);
?? }
?? if ((ComType.Find("submit")>=0))
?? {
??IHTMLElement*? pHElement;
??spInputElement->QueryInterface(IID_IHTMLElement,(void **)&pHElement);
??pHElement->click();???????????????
?? }
}
//--------------------結(jié)束--------------------------------------//?
2.執(zhí)行:
void CDemoDlg::OnOK()
{
?// TODO: Add extra validation here
? ::CoInitialize(NULL); //初始化COM
???? EnumIE();???????????? //枚舉瀏覽器??????
???? ::CoUninitialize();?? //釋放COM
?//CDialog::OnOK();
}
總結(jié)
以上是生活随笔為你收集整理的vc遍历网页表单并自动填写提交的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生产系统支撑终端故障处理的三个误区
- 下一篇: 四季恋语