描述:
要求读取网页输入框(具有输入焦点)的文本内容,我不太懂HMTL脚本,希望通过COM来做,通过IHTMLDocument之类接口.
解决方案1:
void CDoHTMLAppDlg::OnSpy()
{
// TODO: Add your control notification handler code here
SHDocVw::IShellWindowsPtr m_spSHWinds;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
if(m_spSHWinds==NULL)
{
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows))!=S_OK)
{
AfxMessageBox("失败...",MB_ICONINFORMATION);
CoUninitialize();
}
}
if(m_spSHWinds)
{
int n=m_spSHWinds->GetCount();
for(int i=0;i<n;i++)
{
_variant_t v=(long)i;
IDispatchPtr spDisp=m_spSHWinds->Item(v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);//生成一个IE窗口的智能指针
if(spBrowser)
{
//
VARIANT id,index;
if(SUCCEEDED(spBrowser->get_Document(&spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
if(SUCCEEDED(pDoc2->get_forms(&pElementCol)))
{
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
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_IHTMLFormElement,(void**)&pFormElement)))
{
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)))
for(long j=0;j<=(q-1);j++)
{
V_VT(&id)=VT_I4;
V_I4(&id)=j;
V_VT(&index)=VT_I4;
V_I4(&index)=0;
if(SUCCEEDED(pFormElement->item(id,index,&spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
{
CComBSTR value;
CComBSTR type;
pInputElement->get_type(&type);
CString strtype(type);
strtype.MakeUpper();
if(strtype.Find("TEXT")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
AfxMessageBox(_bstr_t(value),MB_ICONINFORMATION);//获取文本吧!
}
else if(strtype.Find("PASSWORD")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
AfxMessageBox(_bstr_t(value)+_bstr_t("密码"),MB_ICONINFORMATION);
}
}
}
}
}
}
}
}
//H
_bstr_t bsName=spBrowser->GetLocationName();//窗口名称
AfxMessageBox(bsName,MB_ICONINFORMATION);
spBrowser->AddRef();
void * pData=spBrowser;
AfxMessageBox((DWORD)(pData),MB_ICONINFORMATION);
}
}
}
}