vc遍历网页表单并自动填写提交 .

简介: 1.独立代码 //-----------开始---------------------//#include #include #include #include #include void EnumIE(void);//处理网页void EnumFrame(IHTMLDocument2 * pIH...

1.独立代码

//-----------开始---------------------//
#include <atlbase.h>
#include <Mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//处理网页
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;    //取得浏览器实例个数(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;  //设置的URL(必须是此URL的网站才有效);
    cIEUrl_Filter="http://127.0.0.1/SmtCCS_manage/"; //设置过滤的网址
    //*******************************************************************************

    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)
 {
     // 程序运行到此,已经找到了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上的子框架frame  

  HRESULT hr;
     
  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"); //未知类型 
  
   EnumField(spInputElement,lpType,lpVal,lpName);//传递并处理表单域的类型、值、名
 }//表单域循环结束    
  }//表单循环结束      
}  

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();               
   }
}
//--------------------结束--------------------------------------// 

2.执行:

void CDemoDlg::OnOK()
{
 // TODO: Add extra validation here
  ::CoInitialize(NULL); //初始化COM
     EnumIE();             //枚举浏览器      
     ::CoUninitialize();   //释放COM
 //CDialog::OnOK();
}

目录
相关文章
|
Java 测试技术 开发者
阿里巴巴Java开发手册(终极版)
别人都说我们是码农,但我们知道,自己是个艺术家。也许我们不过多在意自己的外表和穿着,但我们不羁的外表下,骨子里追求着代码的美、质量的美。而代码规约其实就是一个对美的定义。
3186 0
|
Java
力扣——933. 最近的请求次数(Java、C实现)
力扣——933. 最近的请求次数(Java、C实现)
257 0
力扣——933. 最近的请求次数(Java、C实现)
|
存储 安全 关系型数据库
MySQL的安全性维护
MySQL安全管理
234 0
|
移动开发 前端开发 搜索推荐
H5十大新特性(前端面试新手必背)(2)
H5十大新特性(前端面试新手必背)(2)
153 0
H5十大新特性(前端面试新手必背)(2)
|
9天前
|
人工智能 运维 安全
|
7天前
|
人工智能 异构计算
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
敬请锁定《C位面对面》,洞察通用计算如何在AI时代持续赋能企业创新,助力业务发展!
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
B站开源IndexTTS2,用极致表现力颠覆听觉体验
在语音合成技术不断演进的背景下,早期版本的IndexTTS虽然在多场景应用中展现出良好的表现,但在情感表达的细腻度与时长控制的精准性方面仍存在提升空间。为了解决这些问题,并进一步推动零样本语音合成在实际场景中的落地能力,B站语音团队对模型架构与训练策略进行了深度优化,推出了全新一代语音合成模型——IndexTTS2 。
671 23
|
7天前
|
人工智能 测试技术 API
智能体(AI Agent)搭建全攻略:从概念到实践的终极指南
在人工智能浪潮中,智能体(AI Agent)正成为变革性技术。它们具备自主决策、环境感知、任务执行等能力,广泛应用于日常任务与商业流程。本文详解智能体概念、架构及七步搭建指南,助你打造专属智能体,迎接智能自动化新时代。
|
14天前
|
人工智能 JavaScript 测试技术
Qwen3-Coder入门教程|10分钟搞定安装配置
Qwen3-Coder 挑战赛简介:无论你是编程小白还是办公达人,都能通过本教程快速上手 Qwen-Code CLI,利用 AI 轻松实现代码编写、文档处理等任务。内容涵盖 API 配置、CLI 安装及多种实用案例,助你提升效率,体验智能编码的乐趣。
1097 110