郁闷:IHTMLDocument2居然找不到某个元素ID

xmzh 2010-06-14 05:58:31
我在一个VC项目中,用HTMLVIEW遍历ID,结果用IHTMLDocument2->get_all(),居然没有找到该ID,但是通过火狐部分代码查看是可以看到该ID值,难道get_all()有漏网之鱼?
用下列方法也是找不到:
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 的接口指针

// 删除下行语句的注释,把浏览器的背景改变看看
// spDocument2->put_bgColor( CComVariant( "green" ) );

EnumForm( spDocument2 ); //枚举所有的表单
}
}
void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 )
{
if ( !pIHTMLDocument2 ) return;

HRESULT hr;

CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
pIHTMLDocument2->get_frames( &spFramesCollection2 ); //取得框架frame的集合
pIHTMLDocument2->get_all()
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 spElement; //取得第 j 项表单域
hr = spFormElement->item( CComVariant( j ), CComVariant(), &spElement );
if ( FAILED( hr ) ) continue;

//====== table ======
//====== div ======
CComVariant vName,vVal,vType,vID; //取得表单域的 名,值,类型
hr = spElement.GetPropertyByName( L"name", &vName );
if( FAILED( hr ) ) continue;
hr = spElement.GetPropertyByName( L"value", &vVal );
if( FAILED( hr ) ) continue;
hr = spElement.GetPropertyByName( L"type", &vType );
if( FAILED( hr ) ) continue;
hr = spElement.GetPropertyByName( L"id", &vID );
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"); //未知类型
LPCTSTR lpID = vID.bstrVal?
OLE2CT( vID.bstrVal ) : _T("NULL"); //未知类型

cout << _T("[") << lpType << _T("] ");
cout << lpName << _T(" = ") << lpVal << " ID:"<<lpID<< endl;
}
//想提交这个表单吗?删除下面语句的注释吧
//pForm->submit();
}
}

谁知道是咋回事么?
...全文
605 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Star_Gamer 2011-11-09
  • 打赏
  • 举报
回复
先确定是否全部接收完成 pIHTMLDocument2->get_readyState(&bstrState)
CString strState = bstrState
if(strState == "complete")
//表示已经接收完成
之后做枚举
xmzh 2010-06-19
  • 打赏
  • 举报
回复
高手呢?
xmzh 2010-06-19
  • 打赏
  • 举报
回复
<body class="RichText" style="font-family: 'Verdana'; FONT-SIZE: 10pt;" contentEditable="true" hideFocus="hidefocus" onload="Init();"/>

这就是能看到的EMAIL内容位置信息,但是找不到相应的TEXTAREA ID
xmzh 2010-06-18
  • 打赏
  • 举报
回复
恩,谢谢skyxie,现在发现用IE8工具中的开发人员工具也不错,但是只能看到body,其它好像什么也看不到
skyxie 2010-06-16
  • 打赏
  • 举报
回复
给你推荐一个工具,IEDeveloperToolbar,选到需要操作的元素,很方便就可以看到它id,以及处理了申明事件之类的.

http://www.microsoft.com/downloads/details.aspx?FamilyID=e59c3964-672d-4511-bb3e-2d5e1db91038&displaylang=en
xmzh 2010-06-16
  • 打赏
  • 举报
回复
另外:

谁能找出HOTMAIL发信中信件内容输入框的ID?
xmzh 2010-06-15
  • 打赏
  • 举报
回复
UP ! this my up!
chaosfun 2010-06-14
  • 打赏
  • 举报
回复
看样子你是枚举表单,但有些输入框并不在表单里边,而且也不是input类型,get_forms是得不到的,得每个元素判断它是不是可以编辑的。

3,055

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧