Skip to content

Commit

Permalink
Adding ability to get current URL from IWebBrowser2 if IHTMLDocument2
Browse files Browse the repository at this point in the history
fails.

Under normal circumstances, the IE driver gets the current URL from the
document loaded in the browser. When IE browses to a non-HTML page, such
as a PDF file with the Adobe Reader plugin installed, getting the document
fails, since there is no "document" in the browser. In that case, fall
back to getting the LocationURL property of the IWebBrowser2 interface.
It's arguable that this is the wrong thing to do, since the driver is
useless without an actual document, but we'll let this work anyway. Fixes
issue SeleniumHQ#5361.
  • Loading branch information
jimevans authored and detro committed Mar 19, 2013
1 parent b21a972 commit aa4d84a
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions cpp/IEDriver/Browser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,20 @@ std::string Browser::GetTitle() {
return title_string;
}

std::string Browser::GetBrowserUrl() {
LOG(TRACE) << "Entering Browser::GetBrowserUrl";

CComBSTR url;
HRESULT hr = this->browser_->get_LocationURL(&url);
if (FAILED(hr)) {
LOGHR(WARN, hr) << "Unable to get current URL, call to IWebBrowser2::get_LocationURL failed";
return "";
}

std::string current_url = CW2A(url, CP_UTF8);
return current_url;
}

HWND Browser::GetWindowHandle() {
LOG(TRACE) << "Entering Browser::GetWindowHandle";

Expand Down
1 change: 1 addition & 0 deletions cpp/IEDriver/Browser.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class Browser : public DocumentHost, public IDispEventSimpleImpl<1, Browser, &DI
void GetDocument(IHTMLDocument2** doc);
std::string GetWindowName(void);
std::string GetTitle(void);
std::string GetBrowserUrl(void);
HWND GetWindowHandle(void);
HWND GetTopLevelWindowHandle(void);
HWND GetActiveDialogWindowHandle(void);
Expand Down
5 changes: 3 additions & 2 deletions cpp/IEDriver/DocumentHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ std::string DocumentHost::GetCurrentUrl() {
CComPtr<IHTMLDocument2> doc;
this->GetDocument(&doc);
if (!doc) {
LOG(WARN) << "Unable to get document object, DocumentHost::GetDocument returned NULL";
return "";
LOG(WARN) << "Unable to get document object, DocumentHost::GetDocument returned NULL. "
<< "Attempting to get URL from IWebBrowser2 object";
return this->GetBrowserUrl();
}

CComBSTR url;
Expand Down
1 change: 1 addition & 0 deletions cpp/IEDriver/DocumentHost.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class DocumentHost {
virtual HWND GetWindowHandle(void) = 0;
virtual std::string GetWindowName(void) = 0;
virtual std::string GetTitle(void) = 0;
virtual std::string GetBrowserUrl(void) = 0;
virtual HWND GetActiveDialogWindowHandle(void) = 0;
virtual HWND GetTopLevelWindowHandle(void) = 0;

Expand Down
4 changes: 4 additions & 0 deletions cpp/IEDriver/HtmlDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ std::string HtmlDialog::GetWindowName() {
return "";
}

std::string HtmlDialog::GetBrowserUrl() {
return "";
}

std::string HtmlDialog::GetTitle() {
CComPtr<IHTMLDocument2> doc;
this->GetDocument(&doc);
Expand Down
5 changes: 3 additions & 2 deletions cpp/IEDriver/HtmlDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ struct DialogWindowInfo {
};

class HtmlDialog : public DocumentHost, public IDispEventSimpleImpl<1, HtmlDialog, &DIID_HTMLWindowEvents2> {
public:
public:
HtmlDialog(IHTMLWindow2* window, HWND hwnd, HWND session_handle);
virtual ~HtmlDialog(void);

Expand All @@ -56,6 +56,7 @@ class HtmlDialog : public DocumentHost, public IDispEventSimpleImpl<1, HtmlDialo
HWND GetWindowHandle(void);
std::string GetWindowName(void);
std::string GetTitle(void);
std::string GetBrowserUrl(void);
HWND GetActiveDialogWindowHandle(void);
HWND GetTopLevelWindowHandle(void);

Expand All @@ -69,7 +70,7 @@ class HtmlDialog : public DocumentHost, public IDispEventSimpleImpl<1, HtmlDialo
int NavigateForward(void);
int Refresh(void);

private:
private:
static BOOL CALLBACK FindChildDialogWindow(HWND hwnd, LPARAM arg);

void AttachEvents(void);
Expand Down

0 comments on commit aa4d84a

Please sign in to comment.