Skip to content

Commit c0f5b28

Browse files
committed
Update active_css_js.py to be in sync with recorder_js.py
1 parent 44ca569 commit c0f5b28

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

seleniumbase/js_code/active_css_js.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
var selector = el.nodeName.toLowerCase();
1111
if (el.id) {
1212
elid = el.id;
13-
if (elid.includes(',') || elid.includes('.') ||
14-
elid.includes('(') || elid.includes(')') || hasNumber(elid[0]))
13+
if (elid.includes(',') || elid.includes('.') || /\s/.test(elid) ||
14+
elid.includes('(') || elid.includes(')') || hasDigit(elid[0]))
1515
return cssPathByAttribute(el, 'id');
1616
selector += '#' + elid;
1717
path.unshift(selector);
@@ -87,8 +87,6 @@
8787
return path.join(' > ');
8888
};
8989
var ssOccurrences = function(string, subString, allowOverlapping) {
90-
string += '';
91-
subString += '';
9290
if (subString.length <= 0)
9391
return (string.length + 1);
9492
var n = 0;
@@ -101,9 +99,12 @@
10199
}
102100
return n;
103101
};
104-
function hasNumber(str) {
102+
function hasDigit(str) {
105103
return /\d/.test(str);
106104
};
105+
function isGen(str) {
106+
return /[_-]\d/.test(str);
107+
};
107108
function tagName(el) {
108109
return el.tagName.toLowerCase();
109110
};
@@ -121,10 +122,9 @@
121122
var getBestSelector = function(el) {
122123
if (!(el instanceof Element)) return;
123124
el = turnIntoParentAsNeeded(el);
124-
child_sep = ' > ';
125-
selector_by_id = cssPathById(el);
126-
if (!selector_by_id.includes(child_sep)) return selector_by_id;
127-
child_count_by_id = ssOccurrences(selector_by_id, child_sep);
125+
sel_by_id = cssPathById(el);
126+
if (!sel_by_id.includes(' > ') && !isGen(sel_by_id)) return sel_by_id;
127+
child_count_by_id = ssOccurrences(sel_by_id, ' > ');
128128
selector_by_class = cssPathByClass(el);
129129
tag_name = tagName(el);
130130
non_id_attributes = [];
@@ -145,7 +145,6 @@
145145
non_id_attributes.push('data-cy');
146146
non_id_attributes.push('data-action');
147147
non_id_attributes.push('data-target');
148-
non_id_attributes.push('data-content');
149148
non_id_attributes.push('data-tooltip');
150149
non_id_attributes.push('alt');
151150
non_id_attributes.push('title');
@@ -157,6 +156,7 @@
157156
non_id_attributes.push('ng-href');
158157
non_id_attributes.push('href');
159158
non_id_attributes.push('label');
159+
non_id_attributes.push('data-content');
160160
non_id_attributes.push('class');
161161
non_id_attributes.push('for');
162162
non_id_attributes.push('placeholder');
@@ -175,15 +175,15 @@
175175
else selector_by_attr[i] = cssPathByAttribute(el, n_i_attr);
176176
all_by_attr[i] = document.querySelectorAll(selector_by_attr[i]);
177177
num_by_attr[i] = all_by_attr[i].length;
178-
if (!selector_by_attr[i].includes(child_sep) &&
178+
if (!selector_by_attr[i].includes(' > ') &&
179179
((num_by_attr[i] == 1) || (el == all_by_attr[i][0])))
180180
{
181181
if (n_i_attr == 'aria-label' || n_i_attr == 'for')
182-
if (hasNumber(selector_by_attr[i]))
182+
if (hasDigit(selector_by_attr[i]))
183183
continue;
184184
return selector_by_attr[i];
185185
}
186-
child_count_by_attr[i] = ssOccurrences(selector_by_attr[i], child_sep);
186+
child_count_by_attr[i] = ssOccurrences(selector_by_attr[i], ' > ');
187187
}
188188
basic_tags = [];
189189
basic_tags.push('h1');
@@ -201,23 +201,24 @@
201201
contains_tags = [];
202202
contains_tags.push('a');
203203
contains_tags.push('b');
204-
contains_tags.push('i');
205204
contains_tags.push('h1');
206205
contains_tags.push('h2');
207206
contains_tags.push('h3');
208207
contains_tags.push('h4');
209208
contains_tags.push('h5');
210-
contains_tags.push('li');
211-
contains_tags.push('td');
212-
contains_tags.push('th');
213209
contains_tags.push('code');
214210
contains_tags.push('mark');
215-
contains_tags.push('label');
216-
contains_tags.push('small');
217211
contains_tags.push('button');
212+
contains_tags.push('label');
218213
contains_tags.push('legend');
214+
contains_tags.push('li');
215+
contains_tags.push('td');
216+
contains_tags.push('th');
217+
contains_tags.push('i');
218+
contains_tags.push('small');
219219
contains_tags.push('strong');
220220
contains_tags.push('summary');
221+
contains_tags.push('span');
221222
all_by_tag = [];
222223
text_content = '';
223224
if (el.textContent)
@@ -239,9 +240,9 @@
239240
}
240241
}
241242
}
242-
best_selector = selector_by_id;
243+
best_selector = sel_by_id;
243244
lowest_child_count = child_count_by_id;
244-
child_count_by_class = ssOccurrences(selector_by_class, child_sep);
245+
child_count_by_class = ssOccurrences(selector_by_class, ' > ');
245246
if (child_count_by_class < lowest_child_count) {
246247
best_selector = selector_by_class;
247248
lowest_child_count = child_count_by_class;

0 commit comments

Comments
 (0)