C++ 如何从IE DOM获取最新的IHTMLDocument2对象
目前,我使用MSAA从IE HWND获取IHTMLDocument2对象。但是,对于某些复杂的web应用程序,此IHTMLDocument2对象可能包含多个IHTMLDocument2对象,其中一些对象不属于当前显示页面,而是属于上一页面 在我看来,IE有时不引用它的DOM对象,而是不断地向它的DOM中添加更多的IHTMLDocument2对象。我的问题是如何从DOM对象获取当前显示的IHTMLDocument2对象 提前谢谢 更新 嗨,雷米 谢谢你的回答 是的,你是对的,我确实使用帧来访问其他IHTMLDocument2对象。我的理解是,我从HWND获得的IHTMLDocument2对象是其DOM中的顶级对象。IE有时也会将以前的IHTMLDocument2对象放在其中一个帧中 这是我的部分代码C++ 如何从IE DOM获取最新的IHTMLDocument2对象,c++,internet-explorer,dom,mshtml,C++,Internet Explorer,Dom,Mshtml,目前,我使用MSAA从IE HWND获取IHTMLDocument2对象。但是,对于某些复杂的web应用程序,此IHTMLDocument2对象可能包含多个IHTMLDocument2对象,其中一些对象不属于当前显示页面,而是属于上一页面 在我看来,IE有时不引用它的DOM对象,而是不断地向它的DOM中添加更多的IHTMLDocument2对象。我的问题是如何从DOM对象获取当前显示的IHTMLDocument2对象 提前谢谢 更新 嗨,雷米 谢谢你的回答 是的,你是对的,我确实使用帧来访问其他
BOOL IESpy::GetHTMLText( CComPtr<IHTMLDocument2> spDoc, int tagNo, int schNo)
{
USES_CONVERSION;
HRESULT hr = NULL;
BOOL res = TRUE;
BOOL doneSearch = FALSE;
// Extract the source code of the document
if (spDoc) {
IHTMLFramesCollection2* pFrames = NULL;
if (hr = (spDoc->get_frames(&pFrames)) == S_OK){
LONG framesCount;
pFrames->get_length(&framesCount);
if (framesCount > 0) {
for( long i=0; i < framesCount; i++) {
VARIANT varIdx;
varIdx.vt=VT_I4;
VARIANT varResult;
varIdx.lVal=i;
VariantInit(&varResult);
hr = pFrames->item(&varIdx, &varResult);
if (SUCCEEDED(hr) && (varResult.vt == VT_DISPATCH)){
CComQIPtr<IHTMLWindow2> pFrameWnd;
CComQIPtr<IHTMLDocument2> pFrameDoc;
CComBSTR description=NULL;
pFrameWnd = varResult.pdispVal;
VariantClear(&varResult);
if (pFrameWnd == 0) {
continue;
}
hr = pFrameWnd->get_document(&pFrameDoc);
if (SUCCEEDED(hr) && pFrameDoc){
GetHTMLText( pFrameDoc, tagNo, schNo );
if ( m_foundText ) {
break;
}
} else if ( hr == E_ACCESSDENIED ) {
CComQIPtr<IWebBrowser2> spBrws = HtmlWindowToHtmlWebBrowser(pFrameWnd);
if ( spBrws != NULL) {
// Get the document object from the IWebBrowser2 object.
CComQIPtr<IDispatch> spDisp;
hr = spBrws->get_Document(&spDisp);
if ( hr == S_OK ) {
pFrameDoc = spDisp;
if ( pFrameDoc ) {
GetHTMLText( pFrameDoc, tagNo, schNo );
if ( m_foundText ) {
break;
}
}
}
}
}
}
}
}
pFrames->Release();
if ( !m_foundText ) {
res = ReadSearchText(spDoc, tagNo, schNo );
doneSearch = TRUE;
}
}
if ( !m_foundText && doneSearch == FALSE ) {
res = ReadSearchText(spDoc, tagNo, schNo );
}
}
return res;
}
BOOL IESpy::ReadSearchText(CComPtr<IHTMLDocument2> spDoc, int tagNo, int schNo )
{
USES_CONVERSION;
HRESULT hr = NULL;
BOOL found = FALSE;
IHTMLElementCollection *pAll;
hr = spDoc->get_all(&pAll);
if (FAILED(hr)) {
return FALSE;
}
long items;
IDispatch *ppvDisp;
IHTMLElement *ppvElement;
pAll->get_length(&items);
std::wstring foundText = L"";
for ( long j = 0; j < items; j++ ) {
VARIANT index;
index.vt = VT_I4;
index.lVal = j;
hr = pAll->item( index, index, &ppvDisp );
if (FAILED(hr)) {
return FALSE;
}
if ( ppvDisp ) {
ppvDisp->QueryInterface(IID_IHTMLElement, (void **)&ppvElement);
if ( ppvElement ) {
CComBSTR bstrTag;
ppvElement->get_tagName(&bstrTag);
wchar_t *wtemp = OLE2W(bstrTag);
if ( wtemp ) {
std::wstring text = ReadSearchText(ppvElement, wtemp, tagNo, schNo, found);
if ( !text.empty() ) {
if ( !foundText.empty() ) {
foundText += concat_string;
}
foundText += text;
}
ppvElement->Release();
if ( found ) {
BOOL stop = FALSE;
for ( size_t i = 0; i < m_tagName[tagNo]->size(); i++ ) {
if ( wcscmp(m_tagName[tagNo]->at(i).c_str(), L"HTML") == 0
|| wcscmp(m_tagName[tagNo]->at(i).c_str(), L"HEAD") == 0
|| wcscmp(m_tagName[tagNo]->at(i).c_str(), L"BODY") == 0 ) {
stop = TRUE;
break;
}
}
if ( stop ) {
break;
}
}
} else {
ppvElement->Release();
}
}
}
}
if ( !foundText.empty() ) {
if ( m_screenCompare ) {
// long timeStamp = GetHPTimeStamp(spDoc);
// m_temp_results[timeStamp] = foundText;
m_temp_results.push_back(foundText);
} else {
m_result += foundText;
m_result += L" ";
m_foundText = TRUE;
}
}
return TRUE;
}
BOOL-IESpy::GetHTMLText(CComPtr-spDoc、int-tagNo、int-schNo)
{
使用_转换;
HRESULT hr=NULL;
BOOL res=真;
BOOL-doneSearch=FALSE;
//提取文档的源代码
国际单项体育联合会(spDoc){
IHTMLFramesCollection2*pFrames=NULL;
如果(hr=(spDoc->get_frames(&pFrames))==S_OK){
长框架;
pFrames->get_length(&framescont);
如果(帧扫描>0){
for(长i=0;iitem(&varIdx,&varResult);
if(成功(hr)&(varResult.vt==vt\u调度)){
CComQIPtr-pFrameWnd;
CComQIPtr-pFrameDoc;
CComBSTR description=NULL;
pFrameWnd=varResult.pdispVal;
VariantClear(&varResult);
如果(pFrameWnd==0){
继续;
}
hr=pFrameWnd->get_文档(&pFrameDoc);
if(成功(hr)和PFD){
GetHTMLText(pFrameDoc、tagNo、schNo);
如果(m_foundText){
打破
}
}else if(hr==E_ACCESSDENIED){
CComQIPtr spBrws=HtmlWindowToHtmlWebBrowser(pFrameWnd);
如果(spBrws!=NULL){
//从IWebBrowser2对象获取文档对象。
CComQIPtr-spDisp;
hr=spBrws->get_文档(&spDisp);
如果(hr==S_正常){
pFrameDoc=spDisp;
if(pFrameDoc){
GetHTMLText(pFrameDoc、tagNo、schNo);
如果(m_foundText){
打破
}
}
}
}
}
}
}
}
pFrames->Release();
如果(!m_foundText){
res=ReadSearchText(spDoc、tagNo、schNo);
doneSearch=TRUE;
}
}
如果(!m_foundText&&doneSearch==FALSE){
res=ReadSearchText(spDoc、tagNo、schNo);
}
}
返回res;
}
BOOL-IESpy::ReadSearchText(CComPtr-spDoc、int-tagNo、int-schNo)
{
使用_转换;
HRESULT hr=NULL;
BOOL-found=FALSE;
IHTMLElementCollection*pAll;
hr=spDoc->get_all(&pAll);
如果(失败(小时)){
返回FALSE;
}
长项目;
IDispatch*ppvDisp;
IHTMLElement*ppvElement;
pAll->get_长度(&项目);
std::wstring foundText=L“”;
用于(长j=0;jitem(索引、索引和ppvDisp);
如果(失败(小时)){
返回FALSE;
}
if(ppvDisp){
ppvDisp->QueryInterface(IID_ihtmlement,(void**)和ppvElement);
if(ppvelment){
ccombstrtag;
ppvElement->get_标记名(&bstrTag);
wchar_t*wtemp=OLE2W(bstrTag);
如果(wtemp){
std::wstring text=ReadSearchText(ppvelment、wtemp、tagNo、schNo、found);
如果(!text.empty()){
如果(!foundText.empty()){
foundText+=concat_字符串;
}
foundText+=文本;
}
ppvElement->Release();
如果(找到){
BOOL-stop=FALSE;
对于(size_t i=0;isize();i++){
如果(wcscmp(m_标记名[tagNo]->at(i).c_str(),L“HTML”)==0
||wcscmp(m_标记名[tagNo]->at(i).c_str(),L“HEAD”)==0
||wcscmp(m_标记名[tagNo]->at(i).c_str(),L“BODY”)==0){
停止=真;
打破
}
}
如果(停止){
打破
}
}
}否则{
ppvElement->Release();
}
}
}
}
如果(!foundText.empty()){
如果(m_屏幕比较){
//长时间戳=GetHPTimeStamp(spDo