如何定制一款12306抢票浏览器――实现自动查询和预定功能(四)

2014-11-24 08:18:56 · 作者: · 浏览: 7
CComPtr spEnter_wElem;
hr = GetEnter_wElement(spMainDoc, spEnter_wElem );
CHECKHRPOINTER(hr, spEnter_wElem);
CComPtr spIDGridbox;
hr = GetElementByID( spEnter_wElem, L"gridbox", spIDGridbox);
CHECKHRPOINTER(hr, spIDGridbox);
CComPtr spTable;
hr = GetElementByIndex( spIDGridbox, 0, spTable);
CHECKHRPOINTER(hr, spTable);
CComPtr spTbody;
hr = GetElementByIndex( spTable, 0, spTbody);
CHECKHRPOINTER(hr, spTbody);
CComPtr spTr;
hr = GetElementByIndex( spTbody, 1, spTr);
CHECKHRPOINTER(hr, spTr);
CComPtr spTd;
hr = GetElementByIndex(spTr, 0, spTd);
CHECKHRPOINTER(hr, spTd);
CComPtr spDiv;
hr = GetElementByIndex(spTd, 0, spDiv);
CHECKHRPOINTER(hr, spDiv);
CComPtr spDiv2;
hr = GetElementByIndex(spDiv, 0, spDiv2);
CHECKHRPOINTER(hr, spDiv2);
CComPtr spTable2;
hr = GetElementByIndex(spDiv2, 0, spTable2);
CHECKHRPOINTER(hr, spTable2);
CComPtr spTbody2;
hr = GetElementByIndex(spTable2, 0, spTbody2);
CHECKHRPOINTER(hr, spTbody2);
CComPtr spElemCollection;
hr = GetElementCollection(spTbody2, spElemCollection );
CHECKHRPOINTER(hr, spElemCollection);
long lCount = 0;
hr = spElemCollection->get_length(&lCount);
CHECKHR(hr);
for ( long lindex = 0; lindex < lCount; lindex++ ) {
if ( 0 == lindex ) {
continue;
}
CComVariant VarIndex = lindex;
CComPtr
spDispatchElem;
hr = spElemCollection->item( VarIndex, VarIndex, &spDispatchElem );
CHECKHRPOINTER(hr,spDispatchElem);
CComPtr spChildTr;
hr = spDispatchElem->QueryInterface(IID_IHTMLElement, (LPVOID*)& spChildTr);
CHECKHRPOINTER(hr, spChildTr);
hr = GetQueryInfoInTr( spChildTr );
if ( SUCCEEDED(hr) ) {
// 点击了订购按钮了
break;
}
}
} while (0);
return hr;
}
上述代码执行到第57行时,for循环将逐个读取每列车的信息。为了最快速达到点击“预定”按钮,我将判断的操作放在GetQueryInfoInTr中。
[cpp]
HRESULT CDeal12306WebPage::GetQueryInfoInTr( CComPtr & spElem)
{
HRESULT hr = E_FAIL;
do {
CComPtr spElemCollection;
hr = GetElementCollection(spElem, spElemCollection );
CHECKHRPOINTER(hr, spElemCollection);
long lCount = 0;
hr = spElemCollection->get_length(&lCount);
CHECKHR(hr);
StTrainInfo stTraininfoItem;
for ( long lindex = 0; lindex < lCount; lindex++ ) {
CComVariant VarIndex = lindex;
CComPtr spDispatchElem;
hr = spElemCollection->item( VarIndex, VarIndex, &spDispatchElem );
CHECKHRPOINTER(hr,spDispatchElem);
CComPtr spChildTd;
hr = spDispatchElem->QueryInterface(IID_IHTMLElement, (LPVOID*)& spChildTd);
CHECKHRPOINTER(hr, spChildTd);
hr = GetQueryInfoSubItem( spChildTd, stTraininfoItem, lindex );
CHECKHR(hr);
}
CHECKHR(hr);
CComPtr spTd;
hr = GetElementByIndex( spElem, lCount - 1, spTd);
CHECK