Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/javascript/atoms/locators/css.js
4007 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
goog.require('goog.utils');
29
30
31
/**
32
* Find an element by using a CSS selector
33
*
34
* @param {string} target The selector to search for.
35
* @param {!(Document|Element)} root The document or element to perform the
36
* search under.
37
* @return {Element} The first matching element found in the DOM, or null if no
38
* such element could be found.
39
*/
40
bot.locators.css.single = function (target, root) {
41
if (typeof root['querySelector'] !== 'function' &&
42
// IE8 in non-compatibility mode reports querySelector as an object.
43
goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&
44
!goog.utils.isObject(root['querySelector'])) {
45
throw Error('CSS selection is not supported');
46
}
47
48
if (!target) {
49
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
50
'No selector specified');
51
}
52
53
target = goog.string.trim(target);
54
55
var element;
56
try {
57
element = root.querySelector(target);
58
} catch (e) {
59
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
60
'An invalid or illegal selector was specified');
61
}
62
63
return element && element.nodeType == goog.dom.NodeType.ELEMENT ?
64
/**@type {Element}*/ (element) : null;
65
};
66
67
68
/**
69
* Find all elements matching a CSS selector.
70
*
71
* @param {string} target The selector to search for.
72
* @param {!(Document|Element)} root The document or element to perform the
73
* search under.
74
* @return {!IArrayLike} All matching elements, or an empty list.
75
*/
76
bot.locators.css.many = function (target, root) {
77
if (typeof root['querySelectorAll'] !== 'function' &&
78
// IE8 in non-compatibility mode reports querySelector as an object.
79
goog.userAgent.IE && bot.userAgent.isEngineVersion(8) &&
80
!goog.utils.isObject(root['querySelector'])) {
81
throw Error('CSS selection is not supported');
82
}
83
84
if (!target) {
85
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
86
'No selector specified');
87
}
88
89
target = goog.string.trim(target);
90
91
try {
92
return root.querySelectorAll(target);
93
} catch (e) {
94
throw new bot.Error(bot.ErrorCode.INVALID_SELECTOR_ERROR,
95
'An invalid or illegal selector was specified');
96
}
97
};
98
99