Skip to content

Commit 31c89a3

Browse files
committed
Adding delay and logging on attaching to launched IE
There are times when the two methods used for attaching to a launched instance of Internet Explorer will fail. This commit adds additional logging to help make diagnosing the exact failure points of these circumstances easier.
1 parent f532800 commit 31c89a3

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed

Diff for: cpp/iedriver/BrowserFactory.cpp

+25-5
Original file line numberDiff line numberDiff line change
@@ -306,10 +306,12 @@ bool BrowserFactory::AttachToBrowser(ProcessWindowInfo* process_window_info,
306306
// ShellWindows might fail if there is an IE modal dialog blocking
307307
// execution (unverified).
308308
if (!this->force_shell_windows_api_) {
309+
LOG(DEBUG) << "Using Active Accessibility to find IWebBrowser2 interface";
309310
attached = this->AttachToBrowserUsingActiveAccessibility(process_window_info,
310311
error_message);
311312
if (!attached) {
312-
LOG(DEBUG) << "Failed to find IWebBrowser2 using ActiveAccessibility: " << *error_message;
313+
LOG(DEBUG) << "Failed to find IWebBrowser2 using ActiveAccessibility: "
314+
<< *error_message;
313315
}
314316
}
315317

@@ -359,13 +361,28 @@ bool BrowserFactory::AttachToBrowserUsingActiveAccessibility
359361
process_window_info->dwProcessId,
360362
this->browser_attach_timeout_);
361363
return false;
364+
} else {
365+
LOG(DEBUG) << "Found window handle " << process_window_info->hwndBrowser
366+
<< " for window with class 'Internet Explorer_Server' belonging"
367+
<< " to process with id " << process_window_info->dwProcessId;
362368
}
363369

364370
CComPtr<IHTMLDocument2> document;
365371
if (this->GetDocumentFromWindowHandle(process_window_info->hwndBrowser,
366372
&document)) {
373+
int get_parent_window_retry_count = 8;
367374
CComPtr<IHTMLWindow2> window;
368375
HRESULT hr = document->get_parentWindow(&window);
376+
while (FAILED(hr) && get_parent_window_retry_count > 0) {
377+
// We know we have a valid document. We *should* be able to do a
378+
// document.parentWindow call to get the window. However, on the off-
379+
// chance that the document exists, but IE is slow to initialize all
380+
// of the COM objects and the full DOM, we'll sleep up to 2 seconds,
381+
// retrying to get the parent window.
382+
::Sleep(250);
383+
hr = document->get_parentWindow(&window);
384+
--get_parent_window_retry_count;
385+
}
369386
if (SUCCEEDED(hr)) {
370387
// http://support.microsoft.com/kb/257717
371388
CComPtr<IServiceProvider> provider;
@@ -416,9 +433,6 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
416433
return false;
417434
}
418435

419-
HWND hwnd_browser = NULL;
420-
CComPtr<IWebBrowser2> browser;
421-
422436
clock_t end = clock() + (this->browser_attach_timeout_ / 1000 * CLOCKS_PER_SEC);
423437

424438
CComPtr<IEnumVARIANT> enumerator;
@@ -429,7 +443,7 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
429443
}
430444
enumerator->Reset();
431445
for (CComVariant shell_window_variant;
432-
enumerator->Next(1, &shell_window_variant, nullptr) == S_OK;
446+
enumerator->Next(1, &shell_window_variant, NULL) == S_OK;
433447
shell_window_variant.Clear()) {
434448

435449
if (shell_window_variant.vt != VT_DISPATCH) {
@@ -448,6 +462,12 @@ bool BrowserFactory::AttachToBrowserUsingShellWindows(
448462
&BrowserFactory::FindChildWindowForProcess,
449463
reinterpret_cast<LPARAM>(process_window_info));
450464
if (process_window_info->hwndBrowser != NULL) {
465+
LOG(DEBUG) << "Found window handle "
466+
<< process_window_info->hwndBrowser
467+
<< " for window with class 'Internet Explorer_Server'"
468+
<< " belonging to process with id "
469+
<< process_window_info->dwProcessId;
470+
CComPtr<IWebBrowser2> browser;
451471
hr = shell_window_variant.pdispVal->QueryInterface<IWebBrowser2>(&browser);
452472
if (FAILED(hr)) {
453473
LOGHR(WARN, hr) << "Found browser window using ShellWindows "

0 commit comments

Comments
 (0)