我在一个HTMLVIEW里面加载一个页面,页面里有一个按钮,可是我模拟点击,在执行clikc的时候就会出异常,请大家帮我看看,哪里有问题
IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)GetHtmlDocument();
if ( pHtmlDoc2 )
{
IHTMLElementCollection * pAllElem = NULL;
IHTMLElement *pElem = NULL;
pHtmlDoc2->get_all(&pAllElem);
if( pAllElem != NULL )
{
long p = 0;
VARIANT_BOOL vBool;
BSTR bstr;
VARIANT name;
name.vt = VT_I4;
pAllElem->get_length(&p);
for( int i = 0; i < p; i++)
{
name.lVal = i;
if( pAllElem->item(name, name, (LPDISPATCH*)&pElem) == S_OK )
{
IHTMLInputButtonElement *pInputBtnElem = NULL;
if( pElem->QueryInterface(&pInputBtnElem) == S_OK )
{
pInputBtnElem->get_name(&bstr);
CString strName = _com_util::ConvertBSTRToString(bstr);
if( strName.Compare("btnloginfree") == 0)
{
AfxMessageBox("sdf");
pElem->click();
return;
}
}
}
}
}
}
IHTMLDocument2* pHtmlDoc2 = (IHTMLDocument2*)GetHtmlDocument();
if ( pHtmlDoc2 )
{
IHTMLElementCollection * pAllElem = NULL;
IHTMLElement *pElem = NULL;
pHtmlDoc2->get_all(&pAllElem);
if( pAllElem != NULL )
{
long p = 0;
VARIANT_BOOL vBool;
BSTR bstr;
VARIANT name;
name.vt = VT_I4;
pAllElem->get_length(&p);
for( int i = 0; i < p; i++)
{
name.lVal = i;
if( pAllElem->item(name, name, (LPDISPATCH*)&pElem) == S_OK )
{
IHTMLInputButtonElement *pInputBtnElem = NULL;
if( pElem->QueryInterface(&pInputBtnElem) == S_OK )
{
pInputBtnElem->get_name(&bstr);
CString strName = _com_util::ConvertBSTRToString(bstr);
if( strName.Compare("btnloginfree") == 0)
{
AfxMessageBox("sdf");
pElem->click();
return;
}
}
}
}
}
}
# IHTMLDocument * pDoc = (IHTMLDocument *)GetHtmlDocument();
#
# CComQIPtr<IHTMLDocument3> pDoc3(pDoc);
# CComQIPtr<IHTMLDocument4> pDoc4(pDoc);
# if( pDoc3 && pDoc4 )
# {
# IHTMLElement * pElem = NULL;
# pDoc3->getElementById(_bstr_t(_T("btnTest")), &pElem);
#
# CComQIPtr<IHTMLElement3> pElem3(pElem);
# if( pElem3 )
# {
# CComPtr<IHTMLEventObj> pEvent;
# pDoc4->createEventObject( NULL, &pEvent);
#
# if( pEvent )
# {
# _variant_t vEvent(pEvent);
# VARIANT_BOOL vbSuccess = VARIANT_FALSE;
# pElem3->fireEvent( _bstr_t(_T("onclick"))
# , &vEvent
# , &vbSuccess
# );
# }
#
# }
# }
俺不是回答了吗?
提交按钮也是按钮啊 你要按照你的HTML页面的特点编写改写上面的代码啊
IHTMLFormElement::submit
#define VARIANT_TRUE ((VARIANT_BOOL)-1)
#define VARIANT_FALSE ((VARIANT_BOOL)0)
成功了 , 是VARIANT_TRUE
<ul>
<li class="loginhead">
<select id="freeselect" name="logintype" onchange="showE('mail_suffix')">
<option value="login">会员名</option>
<option value="uid" selected="selected">邮箱名</option>
</select></li>
<li>
<input class="input1" id="username" name="u" value="请输入邮箱名" onfocus="mail_clear(this);">
<div id="mail_suffix" style="display:inline;"><p>@sina.com</p></div></li>
</ul>
<div id="na" style="display: none;">请填写正确用户名</div>
<ul>
<li class="loginhead"><label for="password">密 码</label></li>
<li><input type="password" id="password" name="psw" value="" class="input1" /></li>
</ul>
<script type="text/javascript">createAuthcode();</script>
<ul style="display:none;" id="save1">
<li class="loginhead loginheadsingle"></li>
<li>
<input type="checkbox" name="savelogin" value="" id="savelogin" />
<label for="savelogin">记住会员名</label>
</li>
<li class="getpsw" style="margin-left:20px;"><a href="http://login.sina.com.cn/getpass.html" target="_blank">找回密码</a></li>
</ul>
<ul style="display:block;" id="save2">
<li class="loginhead loginheadsingle"></li>
<li>
<input type="checkbox" id="saveuid" name="savelogin" value="" />
<label for="saveuid">记住邮箱名</label>
</li>
<li class="getpsw" style="margin-left:20px;"><a href="http://login.sina.com.cn/getpass.html" target="_blank">找回密码</a></li>
</ul>
<ul>
<li class="loginhead loginheadsingle"></li>
<li title="对登录信息进行加密传输,防止密码泄露。">
<input type="checkbox" id="ssl_free" name="ssl" value="" />
<label for="ssl_free" class="ssl-label">增强安全性</label>
</li>
</ul>
<input class="freedl1" type="submit" name="btnloginfree" value="登 录"
onmouseover="this.className='freedl2'"
onmousedown="this.className='freedl3'"
onmouseout="this.className='freedl1'" />
</form>
fireEvent这条路没错, 关键是free_onsubmit, 只怕它返回了false看看是什么条件没满足
CComQIPtr<IHTMLDocument3> pDoc3(GetHtmlDocument());
CComQIPtr<IHTMLDocument4> pDoc4(pDoc3); CComPtr<IHTMLElementCollection> pColl;
pDoc3->getElementsByName(_bstr_t("btnloginfree"), &pColl);
if( pColl )
{
LONG lLength = 0L;
pColl->get_length(&lLength); if( lLength > 0 )
{
CComPtr<IDispatch> pElem;
pColl->item( _variant_t(0), _variant_t(0), &pElem);
CComQIPtr<IHTMLElement3> pButton(pElem); if( pButton )
{
CComPtr<IHTMLEventObj> pEvent;
pDoc4->createEventObject( NULL, &pEvent); if( pEvent )
{
_variant_t vEvent(pEvent);
VARIANT_BOOL vbSuccess = VARIANT_FALSE;
pButton->fireEvent( _bstr_t(_T("onclick"))
, &vEvent
, &vbSuccess
);
}
}
}
}
我试了一下, 确实不行, 回去再帮你看。可能是需要在onload后再执行
主意,要自己设置一下这个form的action CComQIPtr<IHTMLDocument3> pDoc3(GetHtmlDocument());
CComQIPtr<IHTMLDocument4> pDoc4(pDoc3); CComPtr<IHTMLElement> pDiv;
pDoc3->getElementById(_bstr_t("free"), &pDiv);
CComQIPtr<IHTMLElement2> pDiv2(pDiv); if( pDiv2 )
{
CComPtr<IHTMLElementCollection> pColl;
pDiv2->getElementsByTagName(_bstr_t("form"), &pColl); if( pColl )
{
LONG lLength = 0L;
pColl->get_length(&lLength); if( lLength > 0 )
{
CComPtr<IDispatch> pElem;
pColl->item( _variant_t(0), _variant_t(0), &pElem);
CComQIPtr<IHTMLFormElement> pForm(pElem); if( pForm )
{
pForm->put_action(_bstr_t("http://mail.sina.com.cn/cgi-bin/login.cgi"));
pForm->submit();
}
}
}
}