MCPcopy Create free account
hub / github.com/SeleniumHQ/selenium / AttachToBrowserUsingShellWindows

Method AttachToBrowserUsingShellWindows

cpp/iedriver/BrowserFactory.cpp:651–736  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

649}
650
651bool BrowserFactory::AttachToBrowserUsingShellWindows(
652 ProcessWindowInfo* process_window_info,
653 std::string* error_message) {
654 LOG(TRACE) << "Entering BrowserFactory::AttachToBrowserUsingShellWindows";
655
656 CComPtr<IShellWindows> shell_windows;
657 HRESULT hr = shell_windows.CoCreateInstance(CLSID_ShellWindows);
658 if (FAILED(hr)) {
659 LOGHR(WARN, hr) << "Unable to create an object using the IShellWindows interface with CoCreateInstance";
660 return false;
661 }
662
663 CComPtr<IUnknown> enumerator_unknown;
664 hr = shell_windows->_NewEnum(&enumerator_unknown);
665 if (FAILED(hr)) {
666 LOGHR(WARN, hr) << "Unable to get enumerator from IShellWindows interface";
667 return false;
668 }
669
670 clock_t end = clock() + (this->browser_attach_timeout_ / 1000 * CLOCKS_PER_SEC);
671
672 CComPtr<IEnumVARIANT> enumerator;
673 enumerator_unknown->QueryInterface<IEnumVARIANT>(&enumerator);
674 while (process_window_info->hwndBrowser == NULL) {
675 if (this->browser_attach_timeout_ > 0 && (clock() > end)) {
676 break;
677 }
678 enumerator->Reset();
679 for (CComVariant shell_window_variant;
680 enumerator->Next(1, &shell_window_variant, NULL) == S_OK;
681 shell_window_variant.Clear()) {
682
683 if (shell_window_variant.vt != VT_DISPATCH) {
684 continue;
685 }
686
687 CComPtr<IShellBrowser> shell_browser;
688 hr = IUnknown_QueryService(shell_window_variant.pdispVal,
689 SID_STopLevelBrowser,
690 IID_PPV_ARGS(&shell_browser));
691 if (shell_browser) {
692 HWND hwnd;
693 hr = shell_browser->GetWindow(&hwnd);
694 if (SUCCEEDED(hr)) {
695 ::EnumChildWindows(hwnd,
696 &BrowserFactory::FindChildWindowForProcess,
697 reinterpret_cast<LPARAM>(process_window_info));
698 if (process_window_info->hwndBrowser != NULL) {
699 LOG(DEBUG) << "Found window handle "
700 << process_window_info->hwndBrowser
701 << " for window with class 'Internet Explorer_Server'"
702 << " belonging to process with id "
703 << process_window_info->dwProcessId;
704 CComPtr<IWebBrowser2> browser;
705 hr = shell_window_variant.pdispVal->QueryInterface<IWebBrowser2>(&browser);
706 if (FAILED(hr)) {
707 LOGHR(WARN, hr) << "Found browser window using ShellWindows "
708 << "API, but QueryInterface for IWebBrowser2 "

Callers 1

AttachToBrowserMethod · 0.95

Calls 3

LOGClass · 0.50
ResetMethod · 0.45
ClearMethod · 0.45

Tested by

no test coverage detected