| 36 | } |
| 37 | |
| 38 | int ElementFinder::FindElement(const IECommandExecutor& executor, |
| 39 | const ElementHandle parent_wrapper, |
| 40 | const std::wstring& mechanism, |
| 41 | const std::wstring& criteria, |
| 42 | Json::Value* found_element) { |
| 43 | LOG(TRACE) << "Entering ElementFinder::FindElement"; |
| 44 | |
| 45 | BrowserHandle browser; |
| 46 | int status_code = executor.GetCurrentBrowser(&browser); |
| 47 | if (status_code == WD_SUCCESS) { |
| 48 | if (mechanism == L"css") { |
| 49 | if (!this->HasNativeCssSelectorEngine(executor)) { |
| 50 | LOG(DEBUG) << "Element location strategy is CSS selectors, but " |
| 51 | << "document does not support CSS selectors. Falling back " |
| 52 | << "to using the Sizzle JavaScript CSS selector engine."; |
| 53 | status_code = this->FindElementUsingSizzle(executor, |
| 54 | parent_wrapper, |
| 55 | criteria, |
| 56 | found_element); |
| 57 | if (status_code != WD_SUCCESS) { |
| 58 | LOG(WARN) << "A JavaScript error was encountered finding elements using Sizzle."; |
| 59 | status_code = ENOSUCHELEMENT; |
| 60 | } |
| 61 | return status_code; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | LOG(DEBUG) << "Using FindElement atom to locate element having " |
| 66 | << LOGWSTRING(mechanism) << " = " |
| 67 | << LOGWSTRING(criteria); |
| 68 | CComPtr<IHTMLDocument2> doc; |
| 69 | browser->GetDocument(&doc); |
| 70 | |
| 71 | std::wstring script_source(L"(function() { return ("); |
| 72 | script_source += atoms::asString(atoms::FIND_ELEMENT); |
| 73 | script_source += L")})();"; |
| 74 | |
| 75 | Script script_wrapper(doc, script_source, 3); |
| 76 | script_wrapper.AddArgument(mechanism); |
| 77 | script_wrapper.AddArgument(criteria); |
| 78 | if (parent_wrapper) { |
| 79 | script_wrapper.AddArgument(parent_wrapper->element()); |
| 80 | } |
| 81 | |
| 82 | status_code = script_wrapper.Execute(); |
| 83 | if (status_code == WD_SUCCESS) { |
| 84 | Json::Value atom_result; |
| 85 | int converted_status_code = script_wrapper.ConvertResultToJsonValue(executor, &atom_result); |
| 86 | if (converted_status_code != WD_SUCCESS) { |
| 87 | LOG(WARN) << "Could not convert return from findElements atom to JSON value"; |
| 88 | status_code = ENOSUCHELEMENT; |
| 89 | } else { |
| 90 | int atom_status_code = atom_result["status"].asInt(); |
| 91 | Json::Value atom_value = atom_result["value"]; |
| 92 | status_code = atom_status_code; |
| 93 | *found_element = atom_result["value"]; |
| 94 | } |
| 95 | } else { |
no test coverage detected