I have a code that fetch all url but i want to make a change to make a mouse simulate click on the iframe window and output the url it redirects to.
Here is the code i tried.
#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;
}
This is raw code sorry for it.
When you get a pointer of IWebBrowser2, like
IWebBrowser2* Browser;
You don't need to simulate mouse click. Just open the page instead. like
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 );