如何定制一款12306抢票浏览器――处理预定页面和验证码自动识别功能(二)
sengerInfoCIter iter )
{
HRESULT hr = E_FAIL;
do {
CComPtr spTr;
hr = GetElementByID( spElem, cstrPassengerID, spTr );
CHECKHRPOINTER(hr, spTr);
hr = SetName(spTr, iter->cstrName);
CHECKHR(hr);
hr = SetCardNo(spTr, iter->cstrCardNo);
CHECKHR(hr);
hr = SetMobileNo(spTr, iter->cstrMobileNo);
CHECKHR(hr);
hr = SetTicket(spTr, iter->cstrTicket);
CHECKHR(hr);
hr = SetCardtype(spTr, iter->cstrCardtype);
CHECKHR(hr);
hr = SetSeat(spTr, iter->ListSeat);
} while (0);
return hr;
}
其中填写姓名的操作很简单,只要找到相应控件,并向该控件中插入文字即可
[cpp]
HRESULT CDeal12306WebPage::SetName( CComPtr & spElem, const CString& cstrName )
{
return SetInputHelper(spElem, cstrName, 4);
}
HRESULT CDeal12306WebPage::SetInputHelper( CComPtr & spElem,
const CString& cstrValue, long lIndex )
{
HRESULT hr = E_FAIL;
do {
CComPtr spTd;
hr = GetElementByIndex( spElem, lIndex, spTd );
CHECKHRPOINTER(hr, spTd);
CComPtr spInputElem;
hr = GetElementByIndex(spTd, 0, spInputElem);
CHECKHRPOINTER(hr, spInputElem);
CComPtr spInput;
hr = spInputElem->QueryInterface(IID_IHTMLInputElement, (LPVOID*)&spInput);
CHECKHRPOINTER(hr, spInput);
hr = spInput->put_value( CComBSTR(cstrValue.GetString()) );
CHECKHR(hr);
} while (0);
return hr;
}
设置席别这类Select选项则稍微复杂点,其实原理是一致的
[cpp]
HRESULT CDeal12306WebPage::SetSeat( CComPtr
& spElem,
const CString& cstrSeat )
{
return SetOptionHelper( spElem, cstrSeat, 2);
}
HRESULT CDeal12306WebPage::SetOptionHelper( CComPtr & spElem,
const CString& cstrValue, long lIndex )
{
HRESULT hr = E_FAIL;
do {
CComPtr spTd;
hr = GetElementByIndex( spElem, lIndex, spTd );
CHECKHRPOINTER(hr, spTd);
CComPtr spSelectElem;
hr = GetElementByIndex(spTd, 0, spSelectElem);
CHECKHRPOINTER(hr, spSelectElem);
hr = SetOptionSelect( spSelectElem, cstrValue);
CHECKHR(hr);
} while (0);
return hr;
}
HRESULT CDeal12306WebPage::SetOptionSelect( CComPtr & spElem, const CString& cstrValue )
{
HRESULT hRes = E_FAIL;
HRESULT hr = E_FAIL;
do {
CComPtr spElemCollection;
hr = GetElementCollection(spElem, spElemCollection );
CHECKHRPOINTER(hr, spElemCollection);
long lCount = 0;
hr = spElemCollection->get_length(&lCount);
CHECKHR(hr);
for ( long lindex = 0; lindex < lCount; lindex++ ) {
CComVariant VarIndex = lindex;
CComPtr spDispatchElem;
hr = spElemCollection->item( VarIndex, VarIndex, &spDispatchElem );
CHECKHRPOINTER(hr,spDispatchElem);
CComPtr spOption;
hr = spDispatchElem->QueryInterface(IID_IHTMLOptionElement, (LPVOID*)& spOption);
if ( FAILED(hr) || NULL == spOption ) {
continue;
}
CComBSTR bstrValue;
hr = spOption->get_value(&bstrValue);
if ( FAILED(hr) ) {
continue;
}