void CNetRadioDlg::OnBnClickedTest() { CString strValue; IHTMLDocument2* pDoc = (IHTMLDocument2*)m_Browser.GetHtmlDocument(); HRESULT hr = S_FALSE; IHTMLElementCollection* pAllElem = NULL; IHTMLElementCollection* pAllScript = NULL; IHTMLElement* pElem = NULL; hr = pDoc->get_scripts(&pAllScript); if(SUCCEEDED(hr) && pAllScript != NULL) { IDispatchPtr pElemDisp = NULL; _variant_t varID(1); _variant_t varIdx(0); hr = pAllScript->item(varID, varIdx, &pElemDisp); if(SUCCEEDED(hr) && pElemDisp != NULL) { hr = pElemDisp->QueryInterface(IID_IHTMLElement,(void**)&pElem); if(SUCCEEDED(hr) && pElem != NULL) { BSTR bstrTagName; hr = pElem->get_outerHTML(&bstrTagName); hr = pElem->get_outerHTML(&bstrTagName); if(SUCCEEDED(hr)) { CString strTagName(bstrTagName); SysFreeString(bstrTagName); MessageBox(strTagName); } } } } hr = pDoc->get_all(&pAllElem); if(SUCCEEDED(hr) && pAllElem != NULL) { IDispatchPtr pElemDisp = NULL; _variant_t varID(_T("body")); _variant_t varIdx(0); hr = pAllElem->item(varID, varIdx, &pElemDisp); if(SUCCEEDED(hr) && pElemDisp != NULL) { MessageBox(_T("OK")); } } }
void IEView::scrollToBottom() { /* IHTMLDocument2 *document = getDocument(); if (document != NULL) { wchar_t *p = NULL; if (SUCCEEDED(document->get_readyState(&p))) { int licznik = 0; do { if (FAILED(document->get_readyState(&p))) { break; } licznik++; if (licznik == 1) break; Sleep(10); } while (!wcscmp(p, L"loading")); } IHTMLWindow2* pWindow = NULL; if (SUCCEEDED(document->get_parentWindow( &pWindow )) && pWindow != NULL) { pWindow->scrollBy( 0, 0x01FFFFFF ); } document->Release(); }*/ IHTMLDocument2 *document = getDocument(); if (document != NULL) { IHTMLElementCollection *collection; IHTMLElement *element; IDispatch *dispatch; if (SUCCEEDED(document->get_all(&collection)) && (collection != NULL)) { long len; if (SUCCEEDED(collection->get_length(&len))) { VARIANT variant; variant.vt = VT_I4; variant.lVal = len-1; if (SUCCEEDED(collection->item(variant, variant, &dispatch)) && (dispatch != NULL)) { if (SUCCEEDED(dispatch->QueryInterface(IID_IHTMLElement,(void**)&element)) && (element != NULL)) { variant.vt = VT_BOOL; variant.boolVal = VARIANT_FALSE; if (SUCCEEDED(element->scrollIntoView(variant))) { } element->Release(); } dispatch->Release(); } } collection->Release(); } IHTMLWindow2* pWindow = NULL; if (SUCCEEDED(document->get_parentWindow( &pWindow )) && (pWindow != NULL)) { pWindow->scrollBy( -0x01FFFFFF, 0x01FFFFFF ); pWindow->Release(); } document->Release(); } }
HRESULT GetElementById(IHTMLDocument2 *pDoc, CString szId, IHTMLElement** pElem) { *pElem = NULL; HRESULT hr = S_OK; BSTR bstrID = szId.AllocSysString(); VARIANT varName, varIndex; varName.vt = VT_BSTR; varName.bstrVal = bstrID; varIndex.vt = VT_I4; varIndex.intVal = 0; IHTMLElementCollection *pColl; hr = pDoc->get_all(&pColl); if (hr == S_OK && pColl) { IDispatch* pDisp; hr = pColl->item(varName, varIndex, &pDisp); if (hr == S_OK && pDisp) { hr = pDisp->QueryInterface(IID_IHTMLElement, (void**)pElem); } } ::SysFreeString(bstrID); return hr; }
void ExplorerElement::getChildren(std::vector<AbstractWebElement*> &children) { children.clear(); IHTMLElement *e; long size; IDispatch *childrenDisp; IHTMLElementCollection *col; HRESULT hr = m_pElement->QueryInterface(IID_IHTMLElement, reinterpret_cast<void**>(&e)); if (!FAILED(hr) && e != NULL) { hr = e->get_children(&childrenDisp); } if (!FAILED(hr)) hr = childrenDisp-> QueryInterface(IID_IHTMLElementCollection, reinterpret_cast<void**>(&col)); if (!FAILED(hr) && col != NULL) hr=col->get_length(&size); if (!FAILED(hr)) { IDispatch *el2; VARIANT v1, v2; v1.vt = VT_NULL; for (int i = 0 ; !FAILED(hr) && i < size; i++) { v2.vt = VT_I4; v2.lVal = i; hr = col->item(v2, v2, &el2); if (!FAILED(hr)) { children.push_back(new ExplorerElement(el2, m_pApp)); el2->Release(); } } } col->Release(); }
void CBrowserDlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL) { // TODO: Add your message handler code here if(logout){ CString u=ie.get_LocationURL(); if(u.Find(L"http://vkontakte.ru/profile.php")==0){ CRect rect; ie.GetWindowRect(rect); IHTMLDocument2 *doc=(IHTMLDocument2*)ie.get_Document(); IHTMLElementCollection* links; doc->get_links(&links); long len=1; links->get_length(&len); for(long i=0;i<len;i++){ IDispatch* _item; _variant_t index = i; HRESULT r=links->item(index, index, &_item); IHTMLAnchorElement* item; _item->QueryInterface(__uuidof(IHTMLAnchorElement), (void**)&item); IHTMLElement* __item; _item->QueryInterface(__uuidof(IHTMLElement), (void**)&__item); wchar_t* href; item->get_href(&href); std::wstring whref=href; if(whref.substr(0, 19).compare(L"http://login.vk.com")==0){ __item->click(); logoutClicked=true; TRACE("Clicked!\n"); return; } //delete href; } } } }
HRESULT GetElementByAttribute(IHTMLDocument2 *pDoc, CString szAttributeName, CString szAttributeValue, IHTMLElement** pElem) { IHTMLElementCollection *pColl; HRESULT hr = pDoc->get_all(&pColl); *pElem = NULL; long len; pColl->get_length(&len); for (int i = 0; i < len; i++) { VARIANT varName, varIndex; varName.vt = VT_UINT; varName.lVal = i; varIndex.vt = VT_I4; varIndex.intVal = 0; IDispatch* pDisp; hr = pColl->item(varName, varIndex, &pDisp); if (hr == S_OK) { hr = pDisp->QueryInterface( IID_IHTMLElement, (void**)pElem); if (hr == S_OK) { VARIANT varAttr; varAttr.vt = VT_BSTR; BSTR bstrAttributeName = szAttributeName.AllocSysString(); hr = (*pElem)->getAttribute(bstrAttributeName, 0, &varAttr); ::SysFreeString(bstrAttributeName); if (hr == S_OK && varAttr.vt == VT_BSTR) { CString strAttrValue(varAttr.bstrVal); if (strAttrValue == szAttributeValue) { break; } } } *pElem = NULL; } } return hr; }
HRESULT GetElementByInnerHtml(IHTMLDocument2 *pDoc, CString szInnerHtml, IHTMLElement** pElem) { IHTMLElementCollection *pColl; HRESULT hr = pDoc->get_all(&pColl); *pElem = NULL; long len; pColl->get_length(&len); for (int i = 0; i < len; i++) { VARIANT varName, varIndex; varName.vt = VT_UINT; varName.lVal = i; varIndex.vt = VT_I4; varIndex.intVal = 0; IDispatch* pDisp; hr = pColl->item(varName, varIndex, &pDisp); if (hr == S_OK) { hr = pDisp->QueryInterface( IID_IHTMLElement, (void**)pElem); if (hr == S_OK) { BSTR tempInnerHTML; (*pElem)->get_innerHTML(&tempInnerHTML); if (szInnerHtml == tempInnerHTML) { ::SysFreeString(tempInnerHTML); break; } ::SysFreeString(tempInnerHTML); } *pElem = NULL; } } return hr; }
bool CMyInternetExplorer::FindElement (bool bExactMatch, bool bWildcardMatch, bool bCaseSensitive, bool bClick, bool bTagName, bool bOuterHTML, bool bInnerHTML, bool bTooltip, LPCTSTR sTagNameOrg, LPCTSTR sOuterHTMLOrg, LPCTSTR sInnerHTMLOrg, LPCTSTR sTooltipOrg, CString & sRetTagName, CString & sRetOuterHTML, CString & sRetInnerHTML, CString & sRetTooltip) { ASSERT (m_pWebBrowser != NULL); if (m_pWebBrowser == NULL) return false; HRESULT hr; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2 * pHtmlDoc = NULL; bool bSearch = true; // Retrieve the document object. hr = m_pWebBrowser->get_Document (&pHtmlDocDispatch); if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL)) { hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc); if (SUCCEEDED (hr) && (pHtmlDoc != NULL)) { IHTMLElementCollection* pColl = NULL; hr = pHtmlDoc->get_all (&pColl); if (SUCCEEDED (hr) && (pColl != NULL)) { CString sTagName (sTagNameOrg), sOuterHTML (sOuterHTMLOrg), sInnerHTML (sInnerHTMLOrg), sTooltip (sTooltipOrg); if (!bCaseSensitive) { sTagName.MakeLower (); sOuterHTML.MakeLower (); sInnerHTML.MakeLower (); sTooltip.MakeLower (); } // Obtained the Anchor Collection... long nLength = 0; pColl->get_length (&nLength); for (int i = 0; i < nLength && bSearch; i++) { COleVariant vIdx ((long)i, VT_I4); IDispatch* pElemDispatch = NULL; IHTMLElement * pElem = NULL; hr = pColl->item (vIdx, vIdx, &pElemDispatch); if (SUCCEEDED (hr) && (pElemDispatch != NULL)) { hr = pElemDispatch->QueryInterface(IID_IHTMLElement, (void**)&pElem); if (SUCCEEDED (hr) && (pElem != NULL)) { BSTR bstrTagName, bstrOuterHTML, bstrInnerHTML, bstrTooltip; CString sTempTagName, sTempOuterHTML, sTempInnerHTML, sTempTooltip; if (!FAILED (pElem->get_tagName (&bstrTagName))) { sTempTagName = bstrTagName; SysFreeString (bstrTagName); } if (!FAILED (pElem->get_outerHTML (&bstrOuterHTML))) { sTempOuterHTML = bstrOuterHTML; SysFreeString (bstrOuterHTML); } if (!FAILED (pElem->get_innerHTML (&bstrInnerHTML))) { sTempInnerHTML = bstrInnerHTML; SysFreeString (bstrInnerHTML); } if (!FAILED (pElem->get_title (&bstrTooltip))) { sTempTooltip = bstrTooltip; SysFreeString (bstrTooltip); } sRetTagName = sTempTagName; sRetOuterHTML = sTempOuterHTML; sRetInnerHTML = sTempInnerHTML; sRetTooltip = sTempTooltip; if (!bCaseSensitive) { sTempTagName.MakeLower (); sTempOuterHTML.MakeLower (); sTempInnerHTML.MakeLower (); sTempTooltip.MakeLower (); } // Do the comparison here! bool bMatches = true; if (bMatches && bTagName) { if (bExactMatch) { if (sTempTagName != sTagName) bMatches = false; } else if (bWildcardMatch) { if (bCaseSensitive) { if (!StringHelper::WildcardCompareNoCase (sTagName, sTempTagName)) bMatches = false; } else { if (!StringHelper::WildcardCompare (sTagName, sTempTagName)) bMatches = false; } } // Just locate the text inside! else { if (sTempTagName.Find (sTagName) == -1) bMatches = false; } } if (bMatches && bOuterHTML) { if (bExactMatch) { if (sTempOuterHTML != sOuterHTML) bMatches = false; } else if (bWildcardMatch) { if (bCaseSensitive) { if (!StringHelper::WildcardCompareNoCase (sOuterHTML, sTempOuterHTML)) bMatches = false; } else { if (!StringHelper::WildcardCompare (sOuterHTML, sTempOuterHTML)) bMatches = false; } } // Just locate the text inside! else { if (sTempOuterHTML.Find (sOuterHTML) == -1) bMatches = false; } } if (bMatches && bInnerHTML) { if (bExactMatch) { if (sTempInnerHTML != sInnerHTML) bMatches = false; } else if (bWildcardMatch) { if (bCaseSensitive) { if (!StringHelper::WildcardCompareNoCase (sInnerHTML, sTempInnerHTML)) bMatches = false; } else { if (!StringHelper::WildcardCompare (sInnerHTML, sTempInnerHTML)) bMatches = false; } } // Just locate the text inside! else { if (sTempInnerHTML.Find (sInnerHTML) == -1) bMatches = false; } } if (bMatches && bTooltip) { if (bExactMatch) { if (sTempTooltip != sTooltip) bMatches = false; } else if (bWildcardMatch) { if (bCaseSensitive) { if (!StringHelper::WildcardCompareNoCase (sTooltip, sTempTooltip)) bMatches = false; } else { if (!StringHelper::WildcardCompare (sTooltip, sTempTooltip)) bMatches = false; } } // Just locate the text inside! else { if (sTempTooltip.Find (sTooltip) == -1) bMatches = false; } } // Done with the comparisons! if (bMatches) { //if (IDCANCEL == AfxMessageBox (_T ("Tag: ") + sTempTagName + _T ("\r\nOuter: ") + sTempOuterHTML + _T ("\r\nInner: ") + sTempInnerHTML + _T ("\r\nTooltip: ") + sTempTooltip, MB_OKCANCEL)) // bSearch = false; // No need to search more! bSearch = false; if (bClick) pElem->click (); } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } sRetTagName.Empty (); sRetOuterHTML.Empty (); sRetInnerHTML.Empty (); sRetTooltip.Empty (); if (bSearch == false) return true; return false; }
bool CMyInternetExplorer::FindOption (bool bClick, bool bValue, bool bText, LPCTSTR sValue, LPCTSTR sText) { ASSERT (m_pWebBrowser != NULL); if (m_pWebBrowser == NULL) return false; HRESULT hr; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2 * pHtmlDoc = NULL; bool bSearch = true; // Retrieve the document object. hr = m_pWebBrowser->get_Document (&pHtmlDocDispatch); if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL)) { hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc); if (SUCCEEDED (hr) && (pHtmlDoc != NULL)) { IHTMLElementCollection* pColl = NULL; hr = pHtmlDoc->get_all (&pColl); if (SUCCEEDED (hr) && (pColl != NULL)) { // Obtained the Anchor Collection... long nLength = 0; pColl->get_length (&nLength); for (int i = 0; i < nLength && bSearch; i++) { COleVariant vIdx ((long)i, VT_I4); IDispatch* pElemDispatch = NULL; IHTMLElement * pElem = NULL; hr = pColl->item (vIdx, vIdx, &pElemDispatch); if (SUCCEEDED (hr) && (pElemDispatch != NULL)) { hr = pElemDispatch->QueryInterface(IID_IHTMLElement, (void**)&pElem); if (SUCCEEDED (hr) && (pElem != NULL)) { BSTR bstrTagName; CString sTempTagName; if (!FAILED (pElem->get_tagName (&bstrTagName))) { sTempTagName = bstrTagName; sTempTagName.MakeLower (); SysFreeString (bstrTagName); } if (sTempTagName == _T ("option")) { IHTMLOptionElement * pOption = NULL; hr = pElemDispatch->QueryInterface (IID_IHTMLOptionElement, (void**)&pOption); if (SUCCEEDED (hr) && (pOption != NULL)) { BSTR bstrValue, bstrText; CString sTempValue, sTempText; if (!FAILED (pOption->get_value (&bstrValue))) { sTempValue = bstrValue; SysFreeString (bstrValue); } if (!FAILED (pOption->get_text (&bstrText))) { sTempText = bstrText; SysFreeString (bstrText); } //if (IDCANCEL == AfxMessageBox (_T ("Value: ") + sTempValue + _T ("\r\nText: ") + sTempText, MB_OKCANCEL)) // bSearch = false; // Do the comparison here! bool bMatches = true; if (bMatches && bValue) { if (!StringHelper::WildcardCompareNoCase (sValue, sTempValue)) bMatches = false; } if (bMatches && bText) { if (!StringHelper::WildcardCompareNoCase (sText, sTempText)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; pOption->put_selected (VARIANT_TRUE); if (bClick) pElem->click (); } pOption->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }
bool CMyInternetExplorer::GetInput (IDispatch* pHtmlDocDispatch,bool bClick, bool bSelect, bool bChangeValue, bool bSetCheck, bool bType, bool bName, bool bValue, LPCTSTR sTypeToLook, LPCTSTR sNameToLook, LPCTSTR sValueToLook, bool bNewCheckValue, LPCTSTR sNewValue,LPTSTR szValue) { // ASSERT (m_pWebBrowser != NULL); // if (m_pWebBrowser == NULL) // return false; HRESULT hr; // IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2 * pHtmlDoc = NULL; bool bSearch = true; // Retrieve the document object. // hr = m_pWebBrowser->get_Document (&pHtmlDocDispatch); if (true && (pHtmlDocDispatch != NULL)) { hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc); if (SUCCEEDED (hr) && (pHtmlDoc != NULL)) { IHTMLElementCollection* pColl = NULL; hr = pHtmlDoc->get_all (&pColl); if (SUCCEEDED (hr) && (pColl != NULL)) { // Obtained the Anchor Collection... long nLength = 0; pColl->get_length (&nLength); for (int i = 0; i < nLength && bSearch; i++) { COleVariant vIdx ((long)i, VT_I4); IDispatch* pElemDispatch = NULL; IHTMLElement * pElem = NULL; hr = pColl->item (vIdx, vIdx, &pElemDispatch); if (SUCCEEDED (hr) && (pElemDispatch != NULL)) { hr = pElemDispatch->QueryInterface (IID_IHTMLElement, (void**)&pElem); if (SUCCEEDED (hr) && (pElem != NULL)) { BSTR bstrTagName; CString sTempTagName; if (!FAILED (pElem->get_tagName (&bstrTagName))) { sTempTagName = bstrTagName; sTempTagName.MakeLower (); //AfxMessageBox (sTempTagName); SysFreeString (bstrTagName); } if (sTempTagName == _T ("input")) { IHTMLInputElement * pInputElem = NULL; hr = pElemDispatch->QueryInterface (IID_IHTMLInputElement, (void**)&pInputElem); if (SUCCEEDED (hr) && (pInputElem != NULL)) { BSTR bstrType, bstrName, bstrValue; CString sTempType, sTempName, sTempValue; if (!FAILED (pInputElem->get_type (&bstrType))) { sTempType = bstrType; SysFreeString (bstrType); } if (!FAILED (pInputElem->get_name (&bstrName))) { sTempName = bstrName; SysFreeString (bstrName); } if (!FAILED (pInputElem->get_value (&bstrValue))) { sTempValue = bstrValue; SysFreeString (bstrValue); } //AfxMessageBox (_T ("Name: ") + sTempName + _T ("\r\nType: ") + sTempType + _T ("\r\nValue: ") + sTempValue); // Do the comparison here! lstrcpy(szValue,sTempValue); bool bMatches = true; if (bMatches && bType) { if (!StringHelper::WildcardCompareNoCase (sTypeToLook, sTempType)) bMatches = false; } if (bMatches && bName) { if (!StringHelper::WildcardCompareNoCase (sNameToLook, sTempName)) bMatches = false; } if (bMatches && bValue) { if (!StringHelper::WildcardCompareNoCase (sValueToLook, sTempValue)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; if (bSetCheck) { if (bNewCheckValue) pInputElem->put_checked (VARIANT_TRUE); else pInputElem->put_checked (VARIANT_FALSE); } if (bChangeValue) { CString sTemp (sNewValue); BSTR bstrNewValue = sTemp.AllocSysString (); pInputElem->put_value (bstrNewValue); SysFreeString (bstrNewValue); } if (bSelect) pInputElem->select (); if (bClick) pElem->click (); } pInputElem->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }
bool CMyInternetExplorer::FindAnchor (bool bClick, bool bFocus, bool bName, bool bOuterText, bool bTooltip, bool bURL, LPCTSTR sName, LPCTSTR sOuterText, LPCTSTR sTooltip, LPCTSTR sURL) { ASSERT (m_pWebBrowser != NULL); if (m_pWebBrowser == NULL) return false; HRESULT hr; IDispatch* pHtmlDocDispatch = NULL; IHTMLDocument2 * pHtmlDoc = NULL; bool bSearch = true; // Retrieve the document object. hr = m_pWebBrowser->get_Document (&pHtmlDocDispatch); if (SUCCEEDED (hr) && (pHtmlDocDispatch != NULL)) { hr = pHtmlDocDispatch->QueryInterface (IID_IHTMLDocument2, (void**)&pHtmlDoc); if (SUCCEEDED (hr) && (pHtmlDoc != NULL)) { IHTMLElementCollection* pColl = NULL; hr = pHtmlDoc->get_all (&pColl); if (SUCCEEDED (hr) && (pColl != NULL)) { // Obtained the Anchor Collection... long nLength = 0; pColl->get_length (&nLength); for (int i = 0; i < nLength && bSearch; i++) { COleVariant vIdx ((long)i, VT_I4); IDispatch* pElemDispatch = NULL; IHTMLElement * pElem = NULL; hr = pColl->item (vIdx, vIdx, &pElemDispatch); if (SUCCEEDED (hr) && (pElemDispatch != NULL)) { hr = pElemDispatch->QueryInterface (IID_IHTMLElement, (void**)&pElem); if (SUCCEEDED (hr) && (pElem != NULL)) { BSTR bstrTagName; CString sTempTagName; if (!FAILED (pElem->get_tagName (&bstrTagName))) { sTempTagName = bstrTagName; SysFreeString (bstrTagName); } if (sTempTagName == _T ("a") || sTempTagName == _T ("A")) { IHTMLAnchorElement * pAnchor = NULL; hr = pElemDispatch->QueryInterface(IID_IHTMLAnchorElement, (void**)&pAnchor); if (SUCCEEDED (hr) && (pAnchor != NULL)) { BSTR bstrName, bstrOuterText, bstrURL, bstrTooltip; CString sTempName, sTempOuter, sTempURL, sTempTooltip; if (!FAILED (pElem->get_outerText (&bstrOuterText))) { sTempOuter = bstrOuterText; SysFreeString (bstrOuterText); } if (!FAILED (pElem->get_title (&bstrTooltip))) { sTempTooltip = bstrTooltip; SysFreeString (bstrTooltip); } if (!FAILED (pAnchor->get_name (&bstrName))) { sTempName = bstrName; SysFreeString (bstrName); } if (!FAILED (pAnchor->get_href (&bstrURL))) { sTempURL = bstrURL; SysFreeString (bstrURL); } // Do the comparison here! bool bMatches = true; if (bMatches && bName) { if (!StringHelper::WildcardCompareNoCase (sName, sTempName)) bMatches = false; } if (bMatches && bOuterText) { if (!StringHelper::WildcardCompareNoCase (sOuterText, sTempOuter)) bMatches = false; } if (bMatches && bURL) { if (!StringHelper::WildcardCompareNoCase (sURL, sTempURL)) bMatches = false; } if (bMatches && bTooltip) { if (!StringHelper::WildcardCompareNoCase (sTooltip, sTempTooltip)) bMatches = false; } if (bMatches) { // No need to search more! bSearch = false; if (bFocus) pAnchor->focus (); if (bClick) pElem->click (); } pAnchor->Release (); } } pElem->Release (); } pElemDispatch->Release (); } } pColl->Release (); } pHtmlDoc->Release(); } pHtmlDocDispatch->Release (); } if (bSearch == false) return true; return false; }
IHTMLElement* FindElement(IHTMLDocument2* pDoc2, LPCTSTR elementId, LPCTSTR elementName) { USES_CONVERSION; HRESULT hr; //Enumerate the HTML elements IHTMLElementCollection* pColl = NULL; hr = pDoc2->get_all( &pColl ); if (hr == S_OK && pColl != NULL) { LONG celem; pColl->get_length( &celem ); //Loop through each elment for ( int i=0; i< celem; i++ ) { VARIANT varIndex; varIndex.vt = VT_UINT; varIndex.lVal = i; VARIANT var2; VariantInit( &var2 ); IDispatch* pDisp; hr = pColl->item( varIndex, var2, &pDisp );//Get an element if ( hr == S_OK ) { IHTMLElement* pElem; //Ask for an HTMLElemnt interface hr = pDisp->QueryInterface(IID_IHTMLElement, (void **)&pElem); if ( hr == S_OK ) { if(elementId != NULL) //find element by Id { BSTR bstr; //Get the id of the element pElem->get_id(&bstr); LPCTSTR id = OLE2T(bstr); if(_tcscmp(id, elementId)) { return pElem; } } else if(elementName != NULL) //find element by Name { IHTMLInputButtonElement* pButton; hr = pDisp->QueryInterface(IID_IHTMLInputButtonElement,(void **)&pButton); if ( hr == S_OK ) { BSTR bstr; //Get the name of the element pButton->get_name(&bstr); LPCTSTR name = OLE2T(bstr); if(_tcscmp(name, elementName)) { return pElem; } } } pElem->Release(); } } } pColl->Release(); } return NULL; }