zoukankan      html  css  js  c++  java
  • WebBrowser 中遍历所有的frames

    枚举所有iframe的IWebBrowser2

    // Get the IDispatch of the document.
    //
    LPDISPATCH lpDisp = NULL;
    lpDisp = m_webBrowser.GetDocument();
    
    if (lpDisp)
    {
       IOleContainer* pContainer;
    
       // Get the container.
       //
       HRESULT hr = lpDisp->QueryInterface(IID_IOleContainer,
                                           (void**)&pContainer);
       lpDisp->Release();
    
       if (FAILED(hr))
          return hr;
    
       // Get an enumerator for the frames.
       //
       IEnumUnknown* pEnumerator;
    
       hr = pContainer->EnumObjects(OLECONTF_EMBEDDINGS, &pEnumerator);
       pContainer->Release();
    
       if (FAILED(hr))
          return hr;
    
       IUnknown* pUnk;
       ULONG uFetched;
    
       // Enumerate and refresh all the frames.
       //
       for (UINT i = 0; S_OK == pEnumerator->Next(1, &pUnk, &uFetched); i++)
       {
          // QI for IWebBrowser here to see whether we have 
          // an embedded browser.
          IWebBrowser2* pWebBrowser;
    
          hr = pUnk->QueryInterface(IID_IWebBrowser2, (void**)&pWebBrowser);
          pUnk->Release();
    
          if (SUCCEEDED(hr))
          {
             // Refresh the frame.
             pWebBrowser->Refresh();
             pWebBrowser->Release();
          }
       }
    
       pEnumerator->Release();
    }


    枚举所有iframe的element
    HRESULT hr = S_OK;
    
        try
        {
            CComPtr<IDispatch> spDispDoc;
            hr = m_spWebBrowser->get_Document(&spDispDoc);
    
            // find all iframe elements and filter by id, then insert ad js
            CComQIPtr<IHTMLDocument2> spDoc2 = spDispDoc;
    
            if(spDoc2){
                CComQIPtr<IHTMLElementCollection> spAll;
                hr = spDoc2->get_all(&spAll);
                if (SUCCEEDED(hr)) {
                    CComPtr<IDispatch> spDispObjects;
                    // just handle iframes as example
                    CComVariant varTag = _T("IFRAME"); 
                    varTag.vt = VT_BSTR;
                    hr = spAll->tags(varTag, &spDispObjects);
    
                    if(SUCCEEDED(hr))
                    {
                        CComQIPtr<IHTMLElementCollection> spElementCollection = spDispObjects;
                        long lObjectCount = 0;
                        if( spElementCollection )
                            hr = spElementCollection->get_length(&lObjectCount);if (SUCCEEDED(hr) && lObjectCount > 0)
                        {
                            for (int i = 0; i < lObjectCount; i++)
                            {
                                CComVariant vIndex = i;
                                CComVariant vEmpty;
                                CComPtr<IDispatch> spdispObject;
                                hr = spElementCollection->item(vIndex, vEmpty, &spdispObject);
                                if (SUCCEEDED(hr))
                                {
                                    CComQIPtr<IHTMLElement> spElement = spdispObject;
                                    if (spElement)
                                    {
                                        CComBSTR bstrId = _T("");;
                                        long width = 0, height = 0;
                                        spElement->get_id(&bstrId);
                                        spElement->get_offsetHeight(&height);
                                        spElement->get_offsetWidth(&width);
    
                                        if(!IsAcceptableSize(width, height))
                                            continue;
                         CComVariant vSrcUrl = _T("");
                                        hr = spElement->getAttribute(_T("src"), 0, &vSrcUrl);
     } } } } } } } } catch(_com_error &ex) { LPCTSTR errMsg = ex.ErrorMessage(); WriteLog(_T("_com_error: ") + CString(errMsg)); } return hr;



  • 相关阅读:
    pytest05-参数化
    pytest04-conftest配置文件
    pytest03-fixture
    pytest02-setup和teardown
    SimpleDateFormat 是线程不安全的类,一般不要定义为 static 变量,如果定义为 static ,必须加锁,或者使用 DateUtils 工具类
    线程池不使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式
    线程资源最好通过线程池提供
    获取单例对象需要保证线程安全,其中的方法也要保证线程安全
    高度注意 Map 类集合 K / V 能不能存储 null 值的情况,如下表格
    使用 entrySet 遍历 Map 类集合 KV ,而不是 keySet 方式进行遍历的好处
  • 原文地址:https://www.cnblogs.com/dlbrant/p/3153093.html
Copyright © 2011-2022 走看看