列举IE全部Element的代码

本文提供了一段C++代码,用于枚举Internet Explorer浏览器中所有HTML元素的详细信息,包括标签名、内联文本和元素字符串表示。代码通过使用COM接口和SHDocVw库来获取当前打开的IE窗口,并遍历每个元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//--列举IE全部Element的代码
#include "stdafx.h"
#pragma warning(disable : 4049)
#pragma warning(disable : 4192)
#pragma warning(disable : 4146)
//#import "shdocvw.dll" exclude("tagREADYSTATE")
#import "shdocvw.dll"
//#import "shdocvw.dll" rename_namespace("SHELL_WINDOW")
//using namespace SHELL_WINDOW;
#define SHELL_WINDOW SHDocVw
#include <atlbase.h>
#include <comdef.h>
#include <MSHTML.H>
#include <iostream>
using namespace std;
int IElement(IHTMLDocument2Ptr sp_Doc)
{
 int ret = 0;
 CComPtr<IHTMLElementCollection> pCollection;
 do {
  cout << "---list all IE Element---" << endl;
  //--FAILED
  //if (!SUCCEEDED(sp_Doc->get_links(&pCollection)))
  if (!SUCCEEDED(sp_Doc->get_all(&pCollection)))
  {
   cout << "FAILED." << endl;
   break;
  }
  int count = 0;
  int Count = 0;
  if (!SUCCEEDED(pCollection->get_length((long*)&Count)))
  {
   cout << "FAILED." << endl;
   break;
  }
  for (int i = 0; i < Count; i++)
  {
   _variant_t index = long(i);
   CComPtr<IDispatch> spDispatch;
   if (SUCCEEDED(pCollection->item(index, index, &spDispatch)))
   {
    CComPtr<IHTMLElement> pElement;
    if (SUCCEEDED(spDispatch->QueryInterface(
     __uuidof(IHTMLElement), (void **)&pElement)))
    {
     count ++;
     CComBSTR bstr;
     pElement->get_tagName(&bstr);
     if (bstr.Length() > 0) cout << "tag=" << (char*)_bstr_t(bstr);// << endl;
     pElement->get_innerText(&bstr);
     if (bstr.Length() > 0) cout << "\ttext=" << (char*)_bstr_t(bstr) << endl;
     else cout << "\ttext=" << endl;
     pElement->toString(&bstr);
     if (bstr.Length() > 0) cout << "str=" << (char*)_bstr_t(bstr) << endl;
     //pElement->Release();
    }
    //spDispatch->Release();
   }
  }
  //pCollection->Release();
  cout << "---list count=" << count << "|" << Count << "---" << endl;
  ret = count;
  break;
 } while (false);
 return ret;
}
int main(int argc, char* argv[])
{
 cout << "---test---" << endl;
//--COM
 CoInitialize(NULL);
 
 do {
  cout << "---list all IE Window---" << endl;

  SHELL_WINDOW::IShellWindowsPtr msp_SHWinds = NULL;
  HRESULT hr = msp_SHWinds.CreateInstance(__uuidof(SHELL_WINDOW::ShellWindows));
  if (S_OK != hr || NULL == msp_SHWinds)
  {
   cout << "FAILED." << endl;
   break;
  }
  
  int count = 0;
  int Count = msp_SHWinds->GetCount();
  for (int i = 0; i < Count; i++)
  {
   _variant_t va(long(i), VT_I4);
   IDispatchPtr sp_Disp;
   sp_Disp = msp_SHWinds->Item(va);
   
   //SHELL_WINDOW::IWebBrowser2Ptr sp_Browser = sp_Disp;//--OK
   SHELL_WINDOW::IWebBrowser2Ptr sp_Browser(sp_Disp);
   if (NULL != sp_Browser)
   {
    //cout << (char*)sp_Browser->GetLocationName() << endl;
    IHTMLDocument2Ptr sp_Doc(sp_Browser->GetDocument());
    if (NULL != sp_Doc)
    {
     count ++;
     CComBSTR bstr;
     sp_Doc->get_title(&bstr);
     cout << (char*)_bstr_t(bstr) << endl;
     static int e = 0;
     if (0 == e) e = IElement(sp_Doc);
     
     sp_Doc.Release();
    }
    sp_Browser.Release();
   }
  }
  
  msp_SHWinds.Release();
  cout << "---list count=" << count << "---" << endl;
  break;
 } while (false);
 
 //--COM
 //CoUninitialize();//--???
 OleUninitialize();
 
 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值