先看代码,这部分主要是嵌套的frame源码获取
VARIANT id, index, id1, index1, index2;
IDispatch *spDispatch=0;
IHTMLDocument2 *pDoc=NULL;
IHTMLElement *pElement=0;
IHTMLElementCollection *pElementCol=0;
// IHTMLDivElement *pFormElement=0;
// IHTMLInputElement *pInputElement=0;
IHTMLWindow2 *pHTMLWnd = NULL;
IHTMLDocument2 *pFrameDoc=NULL;
IHTMLFramesCollection2 *pFramesCollection=NULL;
LPDISPATCH lpDispatch;
long p;
VARIANT varindex,varresult;
varresult.vt=VT_DISPATCH;
varindex.vt = VT_I4;
TStringList *Result = new TStringList;
// if ( !CppWebBrowser1->Busy && CppWebBrowser1->Document &&
// AnsiString(CppWebBrowser1->LocationURL) != "about:blank" )
//if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pDoc)) )
if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pDoc)) )
if(pDoc!=NULL)
{
HRESULT hr=pDoc->get_frames(&pFramesCollection);
if(SUCCEEDED(hr)&&pFramesCollection!=NULL)
{
hr=pFramesCollection->get_length(&p);
if(SUCCEEDED(hr))
for(int i=0; i<p; i++)
{
varindex.lVal = i;
if(pFramesCollection->item(&varindex, &varresult) ==S_OK)
{
lpDispatch=(LPDISPATCH)varresult.ppdispVal;
if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd)))
{
VARIANT framerequested;
VARIANT frameout;
IHTMLFramesCollection2* pframescol;
IHTMLWindow2* prightframewindow;
IHTMLDocument2* prightdoc;
//framerequested.vt = VT_BSTR;//若为vt_i4则以索引号来访问
//framerequested.bstrVal = L"printframe";//以名称来访问
framerequested.vt = VT_I4;
framerequested.bstrVal = (BSTR)i;
hr = pHTMLWnd->get_frames(&pframescol);
hr = pframescol->item(&framerequested, &frameout);
//frameout.pdispVal
hr = frameout.pdispVal->QueryInterface(IID_IHTMLWindow2, (void**)&prightframewindow);
hr = prightframewindow->get_document(&prightdoc);
//if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc)))
if (SUCCEEDED(hr))
{
//work with the pFrameDoc
if ( SUCCEEDED ( prightdoc->get_all ( &pElementCol ) ) ) // 得到所有元素
{
long p = 0;
if ( SUCCEEDED ( pElementCol->get_length ( &p ) ) )
{
// 哪一个Frame
if ( p != 0 )
{
for ( long i = 0; i <= p-1; i++ )
{
V_VT ( &id ) = VT_I4;
V_I4 ( &id ) = i;
V_VT ( &index ) = VT_I4;
V_I4 ( &index ) = 0;
if ( SUCCEEDED ( pElementCol->item ( id, index, &spDispatch ) ) )
{
if
(
SUCCEEDED ( spDispatch->QueryInterface ( IID_IHTMLElement,
( void ** ) &pElement ) )
)
{
BSTR Value;
BSTR Type;
BSTR Id;
BSTR Name;
pElement->get_id ( &Id ); // 获取Id的方法不一样
AnsiString tempID;
tempID = AnsiString ( Id ).UpperCase ();
CComBSTR bstrSrc;
pElement->get_outerHTML(&bstrSrc);
// LPTSTR lpStr;
// _variant_t varStr = "测试字符串 ";
// lpStr = _com_util::ConvertBSTRToString( varStr.bstrVal );
// Result->Add(tempID);
/*if (tempID.IsEmpty()) {
}
else */
Result->Add(tempID + "------" + String(bstrSrc));
// if ( tempID == "PRINTTABLE" )
// {
//
//
// // WideString temp = SValue;
// //pInputElement->put_value ( (WideString) sUserName );
// return GetTablesRowsText2(pElement, strPhone1, strPhone2);
// }
}
}
}
}
}
}
}
pHTMLWnd->Release();
pHTMLWnd=NULL;
}
}
}
pFramesCollection->Release();
}
pDoc->Release();
}
return Result;
下面是一层frame源码的获取
VARIANT id, index, id1, index1;
IDispatch *spDispatch=0;
IHTMLDocument2 *pDoc=NULL;
IHTMLElement *pElement=0;
IHTMLElementCollection *pElementCol=0;
// IHTMLDivElement *pFormElement=0;
// IHTMLInputElement *pInputElement=0;
IHTMLWindow2 *pHTMLWnd = NULL;
IHTMLDocument2 *pFrameDoc=NULL;
IHTMLFramesCollection2 *pFramesCollection=NULL;
LPDISPATCH lpDispatch;
long p;
VARIANT varindex,varresult;
varresult.vt=VT_DISPATCH;
varindex.vt = VT_I4;
TStringList *Result = new TStringList;
// if ( !CppWebBrowser1->Busy && CppWebBrowser1->Document &&
// AnsiString(CppWebBrowser1->LocationURL) != "about:blank" )
//if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pDoc)) )
if ( SUCCEEDED(NewBrowser->Document->QueryInterface(IID_IHTMLDocument2, (LPVOID*)&pDoc)) )
if(pDoc!=NULL)
{
HRESULT hr=pDoc->get_frames(&pFramesCollection);
if(SUCCEEDED(hr)&&pFramesCollection!=NULL)
{
hr=pFramesCollection->get_length(&p);
if(SUCCEEDED(hr))
for(int i=0; i<p; i++)
{
varindex.lVal = i;
if(pFramesCollection->item(&varindex, &varresult) ==S_OK)
{
lpDispatch=(LPDISPATCH)varresult.ppdispVal;
if (SUCCEEDED(lpDispatch->QueryInterface(IID_IHTMLWindow2, (LPVOID *)&pHTMLWnd)))
{
if(SUCCEEDED(pHTMLWnd->get_document( &pFrameDoc)))
{
//work with the pFrameDoc
if ( SUCCEEDED ( pFrameDoc->get_all ( &pElementCol ) ) ) // 得到所有元素
{
long p = 0;
if ( SUCCEEDED ( pElementCol->get_length ( &p ) ) )
{
// 哪一个Frame
if ( p != 0 )
{
for ( long i = 0; i <= p-1; i++ )
{
V_VT ( &id ) = VT_I4;
V_I4 ( &id ) = i;
V_VT ( &index ) = VT_I4;
V_I4 ( &index ) = 0;
if ( SUCCEEDED ( pElementCol->item ( id, index, &spDispatch ) ) )
{
if
(
SUCCEEDED ( spDispatch->QueryInterface ( IID_IHTMLElement,
( void ** ) &pElement ) )
)
{
BSTR Value;
BSTR Type;
BSTR Id;
BSTR Name;
pElement->get_id ( &Id ); // 获取Id的方法不一样
AnsiString tempID;
tempID = AnsiString ( Id ).UpperCase ();
CComBSTR bstrSrc;
pElement->get_outerHTML(&bstrSrc);
// LPTSTR lpStr;
// _variant_t varStr = "测试字符串 ";
// lpStr = _com_util::ConvertBSTRToString( varStr.bstrVal );
// Result->Add(tempID);
if (tempID.IsEmpty()) {
}
else
Result->Add(tempID + "------" + String(bstrSrc));
// if ( tempID == "PRINTTABLE" )
// {
//
//
// // WideString temp = SValue;
// //pInputElement->put_value ( (WideString) sUserName );
// return GetTablesRowsText2(pElement, strPhone1, strPhone2);
// }
}
}
}
}
}
}
}
pHTMLWnd->Release();
pHTMLWnd=NULL;
}
}
}
pFramesCollection->Release();
}
pDoc->Release();
}
return Result;