c++ IWebBrowser 模拟单击帧



我有一个获取所有网址的代码,但我想进行更改以使鼠标模拟单击 iframe 窗口并输出它重定向到的 url。

这是我尝试过的代码。

#include <comutil.h>    // _variant_t
#include <mshtml.h>     // IHTMLDocument and IHTMLElement
#include <exdisp.h>     // IWebBrowser2
#include <atlbase.h>    // CComPtr
#include <string>
#include <iostream>
#include <vector>

#pragma comment(lib, "comsuppw.lib")

HRESULT LoadWebpage(
const CComBSTR& webpageURL,
CComPtr<IWebBrowser2>& browser,
CComPtr<IHTMLDocument2>& document)
{
HRESULT hr;
VARIANT empty;
VariantInit(&empty);
// Navigate to the specifed webpage
hr = browser->Navigate(webpageURL, &empty, &empty, &empty, &empty);
//  Wait for the load.
if (SUCCEEDED(hr))
{
    READYSTATE state;
    while (SUCCEEDED(hr = browser->get_ReadyState(&state)))
    {
        if (state == READYSTATE_COMPLETE) break;
    }
}
// The browser now has a document object. Grab it.
if (SUCCEEDED(hr))
{
    CComPtr<IDispatch> dispatch;
    hr = browser->get_Document(&dispatch);
    if (SUCCEEDED(hr) && dispatch != NULL)
    {
        hr = dispatch.QueryInterface<IHTMLDocument2>(&document);
    }
    else
    {
        hr = E_FAIL;
    }
}
return hr;
  }

 void CrawlWebsite(const CComBSTR& webpage, std::vector<std::wstring>& urlList)
 {
    HRESULT hr;
// Create a browser object
CComPtr<IWebBrowser2> browser;
hr = CoCreateInstance(
    CLSID_InternetExplorer,
    NULL,
    CLSCTX_SERVER,
    IID_IWebBrowser2,
    reinterpret_cast<void**>(&browser));
// Grab a web page
CComPtr<IHTMLDocument2> document;
if (SUCCEEDED(hr))
{
    // Make sure these two items are scoped so CoUninitialize doesn't gump
    // us up.
    hr = LoadWebpage(webpage, browser, document);
}
// Grab all the anchors!
if (SUCCEEDED(hr))
{
    CComPtr<IHTMLElementCollection> urls;
    long count = 0;
    hr = document->get_all(&urls);
    if (SUCCEEDED(hr))
    {
        hr = urls->get_length(&count);
    }
    if (SUCCEEDED(hr))
    {
        for (long i = 0; i < count; i++)
        {
            CComPtr<IDispatch>  element;
            CComPtr<IHTMLAnchorElement> anchor;
            // Get an IDispatch interface for the next option.
            _variant_t index = i;
            hr = urls->item(index, index, &element);
            if (SUCCEEDED(hr))
            {
                hr = element->QueryInterface(
                    IID_IHTMLAnchorElement,
                    reinterpret_cast<void **>(&anchor));
            }
            if (SUCCEEDED(hr) && anchor != NULL)
            {
                CComBSTR    url;
                hr = anchor->get_href(&url);
                if (SUCCEEDED(hr) && url != NULL)
                {
                    urlList.push_back(std::wstring(url));
                }
            }
        }
    }
}
 }
     int main()
 {
HRESULT hr;
hr = CoInitialize(NULL);
std::vector<std::wstring>   urls;
CComBSTR webpage(L"http://www.google.com/");

CrawlWebsite(webpage, urls);
for (std::vector<std::wstring>::iterator it = urls.begin();
it != urls.end();
    ++it)
{
    std::wcout << "URL: " << *it << std::endl;

}
getchar();
CoUninitialize();
return 0;

}

这是原始代码,对此感到抱歉。

当你得到一个 IWebBrowser2 的指针时,比如

IWebBrowser2* Browser;

您无需模拟鼠标单击。只需打开页面即可。喜欢

const TCHAR * url =_T("url string");
_bstr_t bsSite;
VARIANT vEmpty;
VariantInit( &vEmpty );
Browser->Stop();
bsSite = url;
HRESULT result = Browser->Navigate( bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty );

相关内容

  • 没有找到相关文章

最新更新