Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
seleniumhq
GitHub Repository: seleniumhq/selenium
Path: blob/trunk/javascript/atoms/locators/css.js
2884 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
6
// "License"); you may not use this file except in compliance
7
// with the License. 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,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied. See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
// TODO: Add support for using sizzle to locate elements
19
20
goog.provide('bot.locators.css');
21
22
goog.require('bot.Error');
23
goog.require('bot.ErrorCode');
24
goog.require('bot.userAgent');
25
goog.require('goog.dom.NodeType');
26
goog.require('goog.string');
27
goog.require('goog.userAgent');
28
29
30
/**
31
* Find an element by using a CSS selector
32
*
33
* @param {string} target The selector to search for.
34
* @param {!(Document|Element)} root The document or element to perform the
35
* search under.
36
* @return {Element} The first matching element found in the DOM, or null if no
37
* such element could be found.
38
*/
39
bot.locators.css.single = function (target, root) {
40
if (!goog.isFunction(root['querySelector']) &&
41
// IE8 in non-compatibility mode reports querySelector as an object.
42
goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&
43
!goog.isObject(root['querySelector'])) {
44
throw Error('CSS selection is not supported');
45
}
46
47
if (!target) {
48
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
49
'No selector specified');
50
}
51
52
target = goog.string.trim(target);
53
54
var element;
55
try {
56
element = root.querySelector(target);
57
} catch (e) {
58
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
59
'An invalid or illegal selector was specified');
60
}
61
62
return element && element.nodeType == goog.dom.NodeType.ELEMENT ?
63
/**@type {Element}*/ (element) : null;
64
};
65
66
67
/**
68
* Find all elements matching a CSS selector.
69
*
70
* @param {string} target The selector to search for.
71
* @param {!(Document|Element)} root The document or element to perform the
72
* search under.
73
* @return {!IArrayLike} All matching elements, or an empty list.
74
*/
75
bot.locators.css.many = function (target, root) {
76
if (!goog.isFunction(root['querySelectorAll']) &&
77
// IE8 in non-compatibility mode reports querySelector as an object.
78
goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&
79
!goog.isObject(root['querySelector'])) {
80
throw Error('CSS selection is not supported');
81
}
82
83
if (!target) {
84
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
85
'No selector specified');
86
}
87
88
target = goog.string.trim(target);
89
90
try {
91
return root.querySelectorAll(target);
92
} catch (e) {
93
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
94
'An invalid or illegal selector was specified');
95
}
96
};
97
98