3,055
社区成员
发帖
与我相关
我的任务
分享
#import <mshtml.tlb> // Internet Explorer 5
#import <shdocvw.dll>
#include "Shlwapi.h "
#pragma comment(lib, "Shlwapi.lib ")
int _tmain(int argc, _TCHAR* argv[])
{
CoInitialize(NULL);
SHDocVw::IShellWindowsPtr m_spSHWinds;
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK)
{
IDispatchPtr spDisp;
long nCount = m_spSHWinds-> GetCount();
for (long i = 0; i < nCount; i++)
{
_variant_t va(i, VT_I4);
spDisp = m_spSHWinds-> Item(va);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (spBrowser != NULL)
{
IDispatchPtr spDisp;
if(spBrowser-> get_Document(&spDisp) == S_OK && spDisp!= 0 )
{
MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp);
MSHTML::IHTMLElementPtr spHtmlElement;
if(spHtmlDocument==NULL)
continue;
spHtmlDocument-> get_body(&spHtmlElement);
if(spHtmlDocument==NULL)
continue;
HRESULT hr;
MSHTML::IHTMLElementCollection* pColl=NULL;
hr=spHtmlDocument-> get_all(&pColl);
if(pColl!=NULL&&SUCCEEDED(hr))
{
long lcount = 0;
pColl-> get_length(&lcount);
for(int i=0;i <lcount;i++)
{
_variant_t index;
index.vt=VT_I4;
index.intVal=i;
IDispatchPtr disp;
disp=pColl-> item(index,index);
if(disp==NULL)
hr=E_FAIL;
else
{
MSHTML::IHTMLInputElementPtr pInput(disp);
if(pInput)
{
BSTR bstrtype;
pInput-> get_type(&bstrtype);
printf(_bstr_t(bstrtype));
if(StrCmpW(bstrtype,L "text ")==0)
{
pInput-> put_value(_bstr_t( "fill it "));
printf( "fill a field\n ");
}
SysFreeString(bstrtype);
}
}
void COpnPageDlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
if(m_pHtmlView)
{
IHTMLDocument2 *pIHTMLDoc=(IHTMLDocument2*)m_pHtmlView->GetHtmlDocument();
if(pIHTMLDoc)
{
EnumFrame(pIHTMLDoc);
CString Str;
CComQIPtr< IHTMLElementCollection > spAllElementCollection;
HRESULT hr = pIHTMLDoc->get_all( &spAllElementCollection ); //
long nAllCount=0;
hr=spAllElementCollection->get_length(&nAllCount);
Str.Format(TEXT("all: %d"),nAllCount);
OutputDebugString(Str);
for(long i = 0 ; i < nAllCount ; i++)
{
IDispatch *pDisp = NULL; //取得第 i 个Link
spAllElementCollection->item( CComVariant(i) , CComVariant() , &pDisp);
if ( pDisp == NULL ) continue;
CComQIPtr< IHTMLElement > spElement = pDisp;
pDisp->Release();
if(!spElement) continue;
CComBSTR bstrSrc;
spElement->get_tagName(&bstrSrc);
CString tStr;
tStr+=bstrSrc.m_str;
tStr+=" ";
/*spElement->get_outerText(&bstrSrc);
tStr+=bstrSrc.m_str;
tStr+=" ";*/
if(_tcsicmp(bstrSrc.m_str,TEXT("A"))==0 || _tcsicmp(bstrSrc.m_str,TEXT("LINK"))==0)
{
spElement->get_outerText(&bstrSrc);
tStr+=bstrSrc.m_str;
tStr+=" ";
/*if(_tcsstr(bstrSrc.m_str,TEXT("同意以下协议"))!=NULL)
{
spElement->click();//这里就是按钮提交
}*/
OutputDebugString(tStr);
}
}
}
}
}
HRESULT COpnPageDlg::EnumFrame(IHTMLDocument2 *pHtmlDoc)
{
if(pHtmlDoc==NULL)
{
return -1;
}
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
pHtmlDoc->get_frames( &spFramesCollection2 ); //取得框架frame的集合
long nFrameCount=0; //取得子框架个数
HRESULT hr = spFramesCollection2->get_length( &nFrameCount );
if(hr>=0 && nFrameCount>0)
{
CString Str;
Str.Format(TEXT("frames:%d"),nFrameCount);
OutputDebugString(Str);
for(int i=0;i<nFrameCount;i++)
{
CComVariant vDispWin2; //取得子框架的自动化接口
hr=spFramesCollection2->item(&CComVariant(i) , &vDispWin2);
//取得子框架的 IHTMLWindow2 接口
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 ) continue;
CComPtr < IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 ); //取得子框架的 IHTMLDocument2 接口
EnumFrame(spDoc2);
CComQIPtr< IHTMLElementCollection > spAllElementCollection;
hr = pHtmlDoc->get_links( &spAllElementCollection ); //
long nAllCount=0;
hr=spAllElementCollection->get_length(&nAllCount);
Str.Format(TEXT("all: %d"),nAllCount);
OutputDebugString(Str);
for(long i = 0 ; i < nAllCount ; i++)
{
IDispatch *pDisp = NULL; //取得第 i 个Link
spAllElementCollection->item( CComVariant(i) , CComVariant() , &pDisp);
if ( pDisp == NULL ) continue;
CComQIPtr< IHTMLElement > spElement = pDisp;
pDisp->Release();
if(!spElement) continue;
CComBSTR bstrSrc;
spElement->get_tagName(&bstrSrc);
CString tStr;
tStr+=bstrSrc.m_str;
tStr+=" ";
if(_tcsicmp(bstrSrc.m_str,TEXT("A"))==0 || _tcsicmp(bstrSrc.m_str,TEXT("LINK"))==0 )
{
spElement->get_outerText(&bstrSrc);
tStr+=bstrSrc.m_str;
tStr+=" ";
}
OutputDebugString(tStr);
}
}
}
return hr;
}