IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;
objDocument=(IHTMLDocument2 *)m_IE.GetDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合
IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);
name.vt=VT_I4;;
for(int i=0;i<a;i++)//遍历所有元素
{
name.lVal = i;
IDispatch * pDispatch=NULL;
HRESULT res = objAllElement->item(name,name,&pDispatch);
if (FAILED(res))
{
continue;
}
IHTMLInputButtonElement *spInputText;
HRESULT rsc = pDispatch->QueryInterface(IID_IHTMLInputButtonElement, (void**)&spInputText);
if (FAILED(rsc))
{
continue;
} BSTR bstrType;
spInputText->get_type(&bstrType);
CString strType(bstrType);
if (strType.CompareNoCase("button") == 0)
{
BSTR bstrVal;
spInputText->get_value(&bstrVal);
CString strVal(bstrVal);
if (strVal.CompareNoCase(" 提交 ") == 0)
{
VARIANT vardisp;
vardisp.vt=VT_DISPATCH;
vardisp.pdispVal=spInputText;
pElem = (IHTMLElement*)spInputText;
HRESULT hr = objDocument->put_onclick(vardisp);
if (FAILED(hr))
{
pElem->click();
continue;
}
break;
}
} }
IHTMLDocument2 *objDocument=NULL;
objDocument=(IHTMLDocument2 *)m_IE.GetDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合
IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);
name.vt=VT_I4;;
for(int i=0;i<a;i++)//遍历所有元素
{
name.lVal = i;
IDispatch * pDispatch=NULL;
HRESULT res = objAllElement->item(name,name,&pDispatch);
if (FAILED(res))
{
continue;
}
IHTMLInputButtonElement *spInputText;
HRESULT rsc = pDispatch->QueryInterface(IID_IHTMLInputButtonElement, (void**)&spInputText);
if (FAILED(rsc))
{
continue;
} BSTR bstrType;
spInputText->get_type(&bstrType);
CString strType(bstrType);
if (strType.CompareNoCase("button") == 0)
{
BSTR bstrVal;
spInputText->get_value(&bstrVal);
CString strVal(bstrVal);
if (strVal.CompareNoCase(" 提交 ") == 0)
{
VARIANT vardisp;
vardisp.vt=VT_DISPATCH;
vardisp.pdispVal=spInputText;
pElem = (IHTMLElement*)spInputText;
HRESULT hr = objDocument->put_onclick(vardisp);
if (FAILED(hr))
{
pElem->click();
continue;
}
break;
}
} }
// pElem = (IHTMLElement*)spInputText; 这个用法不对,要用QueryInterface
IHTMLElement* pElem = NULL;
spInputText->QueryInterface(IID_IHTMLElement, (void**)&pElem);
pElem->click();