我想navigate到某一页面,然后用IHTMLDocument2接口put上我的数据,再提交服务器。
现在是IHTMLDocument2的get_all()得到的标记集合有时是空,有时不是空。代码如下:
__________________________________________
...
...
// web事件接收
...
...
// for example : navigate to a.htm
m_ie.Navigate("a.htm",NULL,NULL,NULL,NULL);// 等待navigate 完成
while ( !m_WebSink.IsNavigateComplete() )
{
Sleep(10);
}
..........................................
// PutData
PutData("name","myname",100);
...
..._________________________________________
void CWebSink::DocumentComplete(...)
{
ODS("DocumentComplete\n");
m_bNavegateComplete = true;
}
void CWebSink::BeforeNavigate2(...)
{
ODS("BeforeNavigate2\n");
m_bNavegateComplete = false;
}
bool CWebSink::IsNavigateComplete()
{
return m_bNavegateComplete;
}void PutData()
{
CComPtr<IDispatch> spDisp;
CComPtr<IDispatch> spDispDoc;
CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> spWb;
CComQIPtr<IHTMLDocument2,&IID_IHTMLDocument2> spDoc;
CComPtr<IHTMLElementCollection> spEleColl;
CComQIPtr<IHTMLInputTextElement,&IID_IHTMLInputTextElement> spInputText;
HRESULT hr=0;
LONG nElementCount=0; spDisp = m_ie.GetApplication();
if ( spDisp != NULL )
{
spWb = spDisp;
if( spWb != NULL )
{
spWb->get_Document(&spDispDoc);
if ( spDispDoc != NULL )
{
spDoc = spDispDoc;
if ( spDoc != NULL )
{
hr = spDoc->get_all(&spEleColl);
if ( hr == S_OK)
{
if ( spEleColl != NULL )
{
spEleColl->get_length(&nElementCount);
for( int i=0; i<nElementCount; i++)
{
CComVariant vtIndex(i);
CComVariant vtEmpty;
CComPtr<IDispatch> spDispEle;
CComQIPtr<IHTMLElement,&IID_IHTMLElement> spEle;
spEleColl->item(vtIndex,vtEmpty,&spDispEle); //tag迭代
if ( spDispEle != NULL)
{
spEle = spDispEle;
if( spEle != NULL )
{
//根据id, or attrib 找到我要找的tag
// put value
}
}
}
}
}
}
}
}
}
}现在的问题是在横线括起来的代码执行时,PutData函数中IHTMLDocument2的get_all得到的元素集全数目是0
而如果以虚线为分界,分别执行比如说分别在两个按钮中执行,函数正常工作,能将指定的值put进去。我想要的是第一种,就是navigate后,马上设置数据
各位来看看
现在是IHTMLDocument2的get_all()得到的标记集合有时是空,有时不是空。代码如下:
__________________________________________
...
...
// web事件接收
...
...
// for example : navigate to a.htm
m_ie.Navigate("a.htm",NULL,NULL,NULL,NULL);// 等待navigate 完成
while ( !m_WebSink.IsNavigateComplete() )
{
Sleep(10);
}
..........................................
// PutData
PutData("name","myname",100);
...
..._________________________________________
void CWebSink::DocumentComplete(...)
{
ODS("DocumentComplete\n");
m_bNavegateComplete = true;
}
void CWebSink::BeforeNavigate2(...)
{
ODS("BeforeNavigate2\n");
m_bNavegateComplete = false;
}
bool CWebSink::IsNavigateComplete()
{
return m_bNavegateComplete;
}void PutData()
{
CComPtr<IDispatch> spDisp;
CComPtr<IDispatch> spDispDoc;
CComQIPtr<IWebBrowser2,&IID_IWebBrowser2> spWb;
CComQIPtr<IHTMLDocument2,&IID_IHTMLDocument2> spDoc;
CComPtr<IHTMLElementCollection> spEleColl;
CComQIPtr<IHTMLInputTextElement,&IID_IHTMLInputTextElement> spInputText;
HRESULT hr=0;
LONG nElementCount=0; spDisp = m_ie.GetApplication();
if ( spDisp != NULL )
{
spWb = spDisp;
if( spWb != NULL )
{
spWb->get_Document(&spDispDoc);
if ( spDispDoc != NULL )
{
spDoc = spDispDoc;
if ( spDoc != NULL )
{
hr = spDoc->get_all(&spEleColl);
if ( hr == S_OK)
{
if ( spEleColl != NULL )
{
spEleColl->get_length(&nElementCount);
for( int i=0; i<nElementCount; i++)
{
CComVariant vtIndex(i);
CComVariant vtEmpty;
CComPtr<IDispatch> spDispEle;
CComQIPtr<IHTMLElement,&IID_IHTMLElement> spEle;
spEleColl->item(vtIndex,vtEmpty,&spDispEle); //tag迭代
if ( spDispEle != NULL)
{
spEle = spDispEle;
if( spEle != NULL )
{
//根据id, or attrib 找到我要找的tag
// put value
}
}
}
}
}
}
}
}
}
}现在的问题是在横线括起来的代码执行时,PutData函数中IHTMLDocument2的get_all得到的元素集全数目是0
而如果以虚线为分界,分别执行比如说分别在两个按钮中执行,函数正常工作,能将指定的值put进去。我想要的是第一种,就是navigate后,马上设置数据
各位来看看
{
Sleep(10);
}
..........................................Sleep(500); //你看看行不
// PutData
PutData("name","myname",100);
其它我已经试过了。While()循环就是延时判断。
已经解决。
都怪自己,对Web事件没仔细看。^_^