如何分析網頁元素,然後進行相應處理,比如填表,遞交等等
#include <Mshtml.h>
AnsiString __fastcall TForm3::Submit(void)
{
IHTMLDocument2 *pHTMLDoc = NULL;
LPDISPATCH pDisp = NULL;
pDisp =CppWebBrowser1->Document;
if(pDisp)
{
if (SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDoc)))
{
pDisp->Release();
IHTMLElementCollection *pelement;
if(pHTMLDoc->get_forms(&pelement)!=S_OK)
{
pHTMLDoc->Release();
pDisp->Release();
pelement->Release();
return "1發送失敗";
}
pDisp->Release();
tagVARIANT a,i;
a.vt=VT_UI4;
a.lVal=0;//這個值為你所要填寫的表單在整個網頁中的順序,0為第一個表單
if(pelement->item(a,i,&pDisp)!=S_OK)
{
pelement->Release();
pDisp->Release();
return "2發送失敗";
}
pelement->Release();
IHTMLFormElement* pFormElem=NULL;
if(pDisp)
{
if(!SUCCEEDED(pDisp->QueryInterface(IID_IHTMLFormElement,(LPVOID*)&pFormElem)))
{
pFormElem->Release();
pDisp->Release();
return "3發送失敗";
}
}
else
{
return "31發送失敗";
}
pDisp->Release();
LPDISPATCH pDisp_msg = NULL;
tagVARIANT phone,msg,empty;
phone.vt=VT_UI4;
phone.lVal=0;//這個值為Input框在表單中的順序
msg.vt=VT_UI4;
msg.lVal=1;//這個值為Text文本框在表單中的順序
if((pFormElem->item(phone,empty,&pDisp)!=S_OK)||(pFormElem->item(msg,empty,&pDisp_msg)!=S_OK))
{
pFormElem->Release();
if(pDisp)
pDisp->Release();
if(pDisp_msg)
pDisp_msg->Release();
return "4發送失敗";
}
IHTMLInputTextElement* phoneElem=NULL;
IHTMLTextAreaElement* msgElem;
if(!pDisp||!pDisp_msg)
return "11失敗";
if (!SUCCEEDED(pDisp->QueryInterface(IID_IHTMLInputTextElement, (void**)&phoneElem))||(!SUCCEEDED(pDisp_msg->QueryInterface(IID_IHTMLTextAreaElement, (void**)&msgElem))))
{
if(phoneElem)
phoneElem->Release();
if(msgElem)
msgElem->Release();
if(pDisp)
pDisp->Release();
if(pDisp_msg)
pDisp_msg->Release();
return "5發送失敗";
}
WideString s_phone,s_msg;
s_phone="888888"
s_msg="你好";
phoneElem->put_value(s_phone.c_bstr());//填寫
msgElem->put_value(s_msg.c_bstr());//填寫
pFormElem->submit();//提交
phoneElem->Release();
msgElem->Release();
pDisp->Release();
pDisp_msg->Release();
pFormElem->Release();
return "發送成功";
}
}
return "發送失敗";
}