Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/cpp/iedriver/ElementFinder.cpp
2867 views
1
// Licensed to the Software Freedom Conservancy (SFC) under one
2
// or more contributor license agreements. See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership. The SFC licenses this file
5
// to you under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "ElementFinder.h"
18
19
#include "errorcodes.h"
20
#include "logging.h"
21
#include "json.h"
22
23
#include "DocumentHost.h"
24
#include "Element.h"
25
#include "Generated/atoms.h"
26
#include "Generated/sizzle.h"
27
#include "IECommandExecutor.h"
28
#include "Script.h"
29
30
namespace webdriver {
31
32
ElementFinder::ElementFinder() {
33
}
34
35
ElementFinder::~ElementFinder() {
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 {
96
// Hitting a JavaScript error with the atom is an unrecoverable
97
// error. The most common case of this for IE is when there is a
98
// page refresh, navigation, or similar, and the driver is polling
99
// for element presence. The calling code can't do anything about
100
// it, so we might as well just log and return the "no such element"
101
// error code. In the common case, this means that the error will be
102
// transitory, and will sort itself out once the DOM returns to normal
103
// after the page transition is completed. Note carefully that this
104
// is an extreme hack, and has the potential to be papering over a
105
// very serious problem in the driver.
106
LOG(WARN) << "A JavaScript error was encountered executing the findElement atom.";
107
status_code = ENOSUCHELEMENT;
108
}
109
} else {
110
LOG(WARN) << "Unable to get browser";
111
}
112
return status_code;
113
}
114
115
int ElementFinder::FindElements(const IECommandExecutor& executor,
116
const ElementHandle parent_wrapper,
117
const std::wstring& mechanism,
118
const std::wstring& criteria,
119
Json::Value* found_elements) {
120
LOG(TRACE) << "Entering ElementFinder::FindElements";
121
122
BrowserHandle browser;
123
int status_code = executor.GetCurrentBrowser(&browser);
124
if (status_code == WD_SUCCESS) {
125
if (mechanism == L"css") {
126
if (!this->HasNativeCssSelectorEngine(executor)) {
127
LOG(DEBUG) << "Element location strategy is CSS selectors, but "
128
<< "document does not support CSS selectors. Falling back "
129
<< "to using the Sizzle JavaScript CSS selector engine.";
130
status_code = this->FindElementsUsingSizzle(executor,
131
parent_wrapper,
132
criteria,
133
found_elements);
134
if (status_code != WD_SUCCESS) {
135
LOG(WARN) << "A JavaScript error was encountered finding elements using Sizzle.";
136
status_code = WD_SUCCESS;
137
*found_elements = Json::Value(Json::arrayValue);
138
}
139
return status_code;
140
}
141
}
142
143
LOG(DEBUG) << "Using FindElements atom to locate element having "
144
<< LOGWSTRING(mechanism) << " = "
145
<< LOGWSTRING(criteria);
146
CComPtr<IHTMLDocument2> doc;
147
browser->GetDocument(&doc);
148
149
std::wstring script_source(L"(function() { return (");
150
script_source += atoms::asString(atoms::FIND_ELEMENTS);
151
script_source += L")})();";
152
153
Script script_wrapper(doc, script_source, 3);
154
script_wrapper.AddArgument(mechanism);
155
script_wrapper.AddArgument(criteria);
156
if (parent_wrapper) {
157
script_wrapper.AddArgument(parent_wrapper->element());
158
}
159
160
status_code = script_wrapper.Execute();
161
if (status_code == WD_SUCCESS) {
162
Json::Value atom_result;
163
int converted_status_code = script_wrapper.ConvertResultToJsonValue(executor, &atom_result);
164
if (converted_status_code != WD_SUCCESS) {
165
LOG(WARN) << "Could not convert return from findElements atom to JSON value";
166
status_code = WD_SUCCESS;
167
*found_elements = Json::Value(Json::arrayValue);
168
} else {
169
int atom_status_code = atom_result["status"].asInt();
170
Json::Value atom_value = atom_result["value"];
171
status_code = atom_status_code;
172
*found_elements = atom_result["value"];
173
}
174
} else {
175
// Hitting a JavaScript error with the atom is an unrecoverable
176
// error. The most common case of this for IE is when there is a
177
// page refresh, navigation, or similar, and the driver is polling
178
// for element presence. The calling code can't do anything about
179
// it, so we might as well just log and return. In the common case,
180
// this means that the error will be transitory, and will sort
181
// itself out once the DOM returns to normal after the page transition
182
// is completed. Return an empty array, and a success error code.
183
LOG(WARN) << "A JavaScript error was encountered executing the findElements atom.";
184
status_code = WD_SUCCESS;
185
*found_elements = Json::Value(Json::arrayValue);
186
}
187
} else {
188
LOG(WARN) << "Unable to get browser";
189
}
190
return status_code;
191
}
192
193
int ElementFinder::FindElementUsingSizzle(const IECommandExecutor& executor,
194
const ElementHandle parent_wrapper,
195
const std::wstring& criteria,
196
Json::Value* found_element) {
197
LOG(TRACE) << "Entering ElementFinder::FindElementUsingSizzle";
198
199
int result;
200
201
BrowserHandle browser;
202
result = executor.GetCurrentBrowser(&browser);
203
if (result != WD_SUCCESS) {
204
LOG(WARN) << "Unable to get browser";
205
return result;
206
}
207
208
std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {");
209
script_source += atoms::asString(atoms::SIZZLE);
210
script_source += L"}\n";
211
script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
212
script_source += L"if (root['querySelector']) { return root.querySelector(arguments[0]); } ";
213
script_source += L"var results = []; Sizzle(arguments[0], root, results);";
214
script_source += L"return results.length > 0 ? results[0] : null;";
215
script_source += L"};})();";
216
217
CComPtr<IHTMLDocument2> doc;
218
browser->GetDocument(&doc);
219
Script script_wrapper(doc, script_source, 2);
220
script_wrapper.AddArgument(criteria);
221
if (parent_wrapper) {
222
CComPtr<IHTMLElement> parent(parent_wrapper->element());
223
script_wrapper.AddArgument(parent);
224
}
225
result = script_wrapper.Execute();
226
227
if (result == WD_SUCCESS) {
228
if (!script_wrapper.ResultIsElement()) {
229
LOG(WARN) << "Found result is not element";
230
result = ENOSUCHELEMENT;
231
} else {
232
result = script_wrapper.ConvertResultToJsonValue(executor,
233
found_element);
234
}
235
} else {
236
LOG(WARN) << "Unable to find elements";
237
result = ENOSUCHELEMENT;
238
}
239
240
return result;
241
}
242
243
int ElementFinder::FindElementsUsingSizzle(const IECommandExecutor& executor,
244
const ElementHandle parent_wrapper,
245
const std::wstring& criteria,
246
Json::Value* found_elements) {
247
LOG(TRACE) << "Entering ElementFinder::FindElementsUsingSizzle";
248
249
int result;
250
251
if (criteria == L"") {
252
// Apparently, Sizzle will happily return an empty array for an empty
253
// string as the selector. We do not want this.
254
return ENOSUCHELEMENT;
255
}
256
257
BrowserHandle browser;
258
result = executor.GetCurrentBrowser(&browser);
259
if (result != WD_SUCCESS) {
260
LOG(WARN) << "Unable to get browser";
261
return result;
262
}
263
264
std::wstring script_source(L"(function() { return function(){ if (!window.Sizzle) {");
265
script_source += atoms::asString(atoms::SIZZLE);
266
script_source += L"}\n";
267
script_source += L"var root = arguments[1] ? arguments[1] : document.documentElement;";
268
script_source += L"if (root['querySelectorAll']) { return root.querySelectorAll(arguments[0]); } ";
269
script_source += L"var results = []; try { Sizzle(arguments[0], root, results); } catch(ex) { results = null; }";
270
script_source += L"return results;";
271
script_source += L"};})();";
272
273
CComPtr<IHTMLDocument2> doc;
274
browser->GetDocument(&doc);
275
276
Script script_wrapper(doc, script_source, 2);
277
script_wrapper.AddArgument(criteria);
278
if (parent_wrapper) {
279
// Use a copy for the parent element?
280
CComPtr<IHTMLElement> parent(parent_wrapper->element());
281
script_wrapper.AddArgument(parent);
282
}
283
284
result = script_wrapper.Execute();
285
if (result == WD_SUCCESS) {
286
CComVariant snapshot = script_wrapper.result();
287
if (snapshot.vt == VT_NULL || snapshot.vt == VT_EMPTY) {
288
// We explicitly caught an error from Sizzle. Return ENOSUCHELEMENT.
289
return ENOSUCHELEMENT;
290
}
291
std::wstring get_element_count_script = L"(function(){return function() {return arguments[0].length;}})();";
292
Script get_element_count_script_wrapper(doc, get_element_count_script, 1);
293
get_element_count_script_wrapper.AddArgument(snapshot);
294
result = get_element_count_script_wrapper.Execute();
295
if (result == WD_SUCCESS) {
296
*found_elements = Json::Value(Json::arrayValue);
297
if (!get_element_count_script_wrapper.ResultIsInteger()) {
298
LOG(WARN) << "Found elements count is not integer";
299
result = EUNEXPECTEDJSERROR;
300
} else {
301
long length = get_element_count_script_wrapper.result().lVal;
302
std::wstring get_next_element_script = L"(function(){return function() {return arguments[0][arguments[1]];}})();";
303
for (long i = 0; i < length; ++i) {
304
Script get_element_script_wrapper(doc, get_next_element_script, 2);
305
get_element_script_wrapper.AddArgument(snapshot);
306
get_element_script_wrapper.AddArgument(i);
307
result = get_element_script_wrapper.Execute();
308
if (result == WD_SUCCESS) {
309
Json::Value json_element;
310
get_element_script_wrapper.ConvertResultToJsonValue(executor,
311
&json_element);
312
found_elements->append(json_element);
313
} else {
314
LOG(WARN) << "Unable to get " << i << " found element";
315
}
316
}
317
}
318
} else {
319
LOG(WARN) << "Unable to get count of found elements";
320
result = EUNEXPECTEDJSERROR;
321
}
322
323
} else {
324
LOG(WARN) << "Execution returned error";
325
}
326
327
return result;
328
}
329
330
bool ElementFinder::HasNativeCssSelectorEngine(const IECommandExecutor& executor) {
331
LOG(TRACE) << "Entering ElementFinder::HasNativeCssSelectorEngine";
332
333
BrowserHandle browser;
334
executor.GetCurrentBrowser(&browser);
335
336
std::wstring script_source(L"(function() { return function(){");
337
script_source += L"var root = document.documentElement;";
338
script_source += L"if (root['querySelectorAll']) { return true; } ";
339
script_source += L"return false;";
340
script_source += L"};})();";
341
342
CComPtr<IHTMLDocument2> doc;
343
browser->GetDocument(&doc);
344
345
Script script_wrapper(doc, script_source, 0);
346
int status_code = script_wrapper.Execute();
347
if (status_code != WD_SUCCESS) {
348
// If executing the script yields an error, then falling back to
349
// Sizzle will never work, so assume there is a native CSS selector
350
// engine.
351
return true;
352
}
353
return script_wrapper.result().boolVal == VARIANT_TRUE;
354
}
355
356
} // namespace webdriver
357
358