Skip to content

Commit c80887f

Browse files
authored
Merge pull request #1038 from seleniumbase/improve-recorder-and-browser-launching
Improve the Recorder and browser-launching
2 parents afc8002 + 967db60 commit c80887f

File tree

4 files changed

+94
-65
lines changed

4 files changed

+94
-65
lines changed

seleniumbase/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
# seleniumbase package
2-
__version__ = "2.0.8"
2+
__version__ = "2.0.9"

seleniumbase/core/browser_launcher.py

Lines changed: 59 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import urllib3
66
import warnings
77
from selenium import webdriver
8+
from selenium.webdriver.chrome.service import Service as Chrome_Service
9+
from selenium.webdriver.edge.service import Service as Edge_Service
10+
from selenium.webdriver.firefox.service import Service as Firefox_Service
811
from seleniumbase.config import settings
912
from seleniumbase.core import download_helper
1013
from seleniumbase.core import proxy_helper
@@ -13,6 +16,9 @@
1316
from seleniumbase import extensions # browser extensions storage folder
1417

1518
urllib3.disable_warnings()
19+
selenium4 = False
20+
if sys.version_info[0] == 3 and sys.version_info[1] >= 7:
21+
selenium4 = True
1622
DRIVER_DIR = os.path.dirname(os.path.realpath(drivers.__file__))
1723
# Make sure that the SeleniumBase DRIVER_DIR is at the top of the System PATH
1824
# (Changes to the System PATH with os.environ only last during the test run)
@@ -116,7 +122,18 @@ def _repair_chromedriver(chrome_options, headless_options):
116122
"sbase install chromedriver 2.44", shell=True
117123
)
118124
try:
119-
driver = webdriver.Chrome(options=headless_options)
125+
if selenium4:
126+
service = Chrome_Service(
127+
executable_path=LOCAL_CHROMEDRIVER)
128+
driver = webdriver.Chrome(
129+
service=service,
130+
options=headless_options,
131+
)
132+
else:
133+
driver = webdriver.Chrome(
134+
executable_path=LOCAL_CHROMEDRIVER,
135+
options=headless_options,
136+
)
120137
except Exception:
121138
subprocess.check_call(
122139
"sbase install chromedriver latest-1", shell=True
@@ -1132,11 +1149,18 @@ def get_local_driver(
11321149
)
11331150
else:
11341151
if os.path.exists(LOCAL_GECKODRIVER):
1135-
warnings.simplefilter("ignore", category=DeprecationWarning)
1136-
return webdriver.Firefox(
1137-
executable_path=LOCAL_GECKODRIVER,
1138-
options=firefox_options,
1139-
)
1152+
if selenium4:
1153+
service = Firefox_Service(
1154+
executable_path=LOCAL_GECKODRIVER)
1155+
return webdriver.Firefox(
1156+
service=service,
1157+
options=firefox_options,
1158+
)
1159+
else:
1160+
return webdriver.Firefox(
1161+
executable_path=LOCAL_GECKODRIVER,
1162+
options=firefox_options,
1163+
)
11401164
else:
11411165
return webdriver.Firefox(options=firefox_options)
11421166
elif browser_name == constants.Browser.INTERNET_EXPLORER:
@@ -1269,10 +1293,6 @@ def get_local_driver(
12691293
sb_install.main(override="edgedriver")
12701294
sys.argv = sys_args # Put back the original sys args
12711295

1272-
selenium4 = False
1273-
if sys.version_info[0] == 3 and sys.version_info[1] >= 7:
1274-
selenium4 = True
1275-
12761296
# For Microsoft Edge (Chromium) version 80 or higher
12771297
if selenium4:
12781298
Edge = webdriver.edge.webdriver.WebDriver
@@ -1410,12 +1430,9 @@ def get_local_driver(
14101430
if len(chromium_arg_item) >= 3:
14111431
edge_options.add_argument(chromium_arg_item)
14121432
if selenium4:
1413-
warnings.simplefilter("ignore", category=DeprecationWarning)
14141433
try:
1415-
driver = Edge(
1416-
executable_path=LOCAL_EDGEDRIVER,
1417-
options=edge_options,
1418-
)
1434+
service = Edge_Service(executable_path=LOCAL_EDGEDRIVER)
1435+
driver = Edge(service=service, options=edge_options)
14191436
except Exception as e:
14201437
auto_upgrade_edgedriver = False
14211438
if "This version of MSEdgeDriver only supports" in e.msg:
@@ -1442,10 +1459,8 @@ def get_local_driver(
14421459
if not _was_chromedriver_repaired(): # Works for Edge
14431460
_repair_edgedriver(edge_version)
14441461
_mark_chromedriver_repaired() # Works for Edge
1445-
driver = Edge(
1446-
executable_path=LOCAL_EDGEDRIVER,
1447-
options=edge_options,
1448-
)
1462+
service = Edge_Service(executable_path=LOCAL_EDGEDRIVER)
1463+
driver = Edge(service=service, options=edge_options)
14491464
return driver
14501465
else:
14511466
capabilities = edge_options.to_capabilities()
@@ -1538,6 +1553,7 @@ def get_local_driver(
15381553
device_pixel_ratio,
15391554
)
15401555
opera_options.headless = False # No support for headless Opera
1556+
warnings.simplefilter("ignore", category=DeprecationWarning)
15411557
return webdriver.Opera(options=opera_options)
15421558
except Exception:
15431559
return webdriver.Opera()
@@ -1619,12 +1635,18 @@ def get_local_driver(
16191635
if not headless or "linux" not in PLATFORM:
16201636
try:
16211637
if os.path.exists(LOCAL_CHROMEDRIVER):
1622-
warnings.simplefilter(
1623-
"ignore", category=DeprecationWarning)
1624-
driver = webdriver.Chrome(
1625-
executable_path=LOCAL_CHROMEDRIVER,
1626-
options=chrome_options,
1627-
)
1638+
if selenium4:
1639+
service = Chrome_Service(
1640+
executable_path=LOCAL_CHROMEDRIVER)
1641+
driver = webdriver.Chrome(
1642+
service=service,
1643+
options=chrome_options,
1644+
)
1645+
else:
1646+
driver = webdriver.Chrome(
1647+
executable_path=LOCAL_CHROMEDRIVER,
1648+
options=chrome_options,
1649+
)
16281650
else:
16291651
driver = webdriver.Chrome(options=chrome_options)
16301652
except Exception as e:
@@ -1692,12 +1714,18 @@ def get_local_driver(
16921714
)
16931715
_mark_chromedriver_repaired()
16941716
if os.path.exists(LOCAL_CHROMEDRIVER):
1695-
warnings.simplefilter(
1696-
"ignore", category=DeprecationWarning)
1697-
driver = webdriver.Chrome(
1698-
executable_path=LOCAL_CHROMEDRIVER,
1699-
options=chrome_options,
1700-
)
1717+
if selenium4:
1718+
service = Chrome_Service(
1719+
executable_path=LOCAL_CHROMEDRIVER)
1720+
driver = webdriver.Chrome(
1721+
service=service,
1722+
options=chrome_options,
1723+
)
1724+
else:
1725+
driver = webdriver.Chrome(
1726+
executable_path=LOCAL_CHROMEDRIVER,
1727+
options=chrome_options,
1728+
)
17011729
else:
17021730
driver = webdriver.Chrome(options=chrome_options)
17031731
return driver

seleniumbase/extensions/recorder.zip

-48 Bytes
Binary file not shown.

seleniumbase/js_code/recorder_js.py

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,25 @@
109109
function hasNumber(str) {
110110
return /\d/.test(str);
111111
};
112+
function tagName(el) {
113+
return el.tagName.toLowerCase();
114+
};
112115
var getBestSelector = function(el) {
113116
if (!(el instanceof Element))
114117
return;
118+
if (tagName(el) == 'span') {
119+
if (tagName(el.parentElement) == 'button')
120+
el = el.parentElement;
121+
else if (tagName(el.parentElement.parentElement) == 'button')
122+
el = el.parentElement.parentElement;
123+
}
115124
child_sep = ' > ';
116125
selector_by_id = cssPathById(el);
117126
if (!selector_by_id.includes(child_sep))
118127
return selector_by_id;
119128
child_count_by_id = ssOccurrences(selector_by_id, child_sep);
120129
selector_by_class = cssPathByClass(el);
121-
tag_name = el.tagName.toLowerCase();
130+
tag_name = tagName(el);
122131
non_id_attributes = [];
123132
non_id_attributes.push('name');
124133
non_id_attributes.push('data-qa');
@@ -230,31 +239,6 @@
230239
}
231240
}
232241
}
233-
if (tag_name == "span" && inner_text.length > 1 && inner_text.length <= 64)
234-
{
235-
parent_element = el.parentElement;
236-
parent_tag_name = parent_element.tagName.toLowerCase();
237-
grand_element = parent_element.parentElement;
238-
grand_tag_name = grand_element.tagName.toLowerCase();
239-
if (parent_tag_name == "button" || grand_tag_name == "button") {
240-
qsa_element = "span";
241-
if (parent_tag_name == "button")
242-
qsa_element = "button > span";
243-
else
244-
qsa_element = "button > "+parent_tag_name+" > span";
245-
t_count = 0;
246-
all_el_found = document.querySelectorAll(qsa_element);
247-
for (var j = 0; j < all_el_found.length; j++) {
248-
if (all_el_found[j].innerText.includes(inner_text))
249-
t_count += 1;
250-
}
251-
if (t_count === 1 && !inner_text.includes('\n')) {
252-
inner_text = inner_text.replaceAll("'", "\\'");
253-
inner_text = inner_text.replaceAll('"', '\\"');
254-
return qsa_element += ':contains("'+inner_text+'")';
255-
}
256-
}
257-
}
258242
best_selector = selector_by_id;
259243
lowest_child_count = child_count_by_id;
260244
child_count_by_class = ssOccurrences(selector_by_class, child_sep);
@@ -370,6 +354,23 @@
370354
document.body.addEventListener('click', function (event) {
371355
// Do Nothing.
372356
});
357+
document.body.addEventListener('submit', function (event) {
358+
reset_if_recorder_undefined();
359+
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
360+
const d_now = Date.now();
361+
ra_len = document.recorded_actions.length;
362+
if (ra_len > 0 &&
363+
document.recorded_actions[ra_len-1][0] === 'input' &&
364+
!document.recorded_actions[ra_len-1][2].endsWith('\n'))
365+
{
366+
selector = document.recorded_actions[ra_len-1][1];
367+
text = document.recorded_actions[ra_len-1][2] + '\n';
368+
document.recorded_actions.pop();
369+
document.recorded_actions.push(['input', selector, text, d_now]);
370+
json_rec_act = JSON.stringify(document.recorded_actions);
371+
sessionStorage.setItem('recorded_actions', json_rec_act);
372+
}
373+
});
373374
document.body.addEventListener('formdata', function (event) {
374375
reset_if_recorder_undefined();
375376
if (sessionStorage.getItem('pause_recorder') === 'yes') return;
@@ -440,7 +441,7 @@
440441
const element = event.target;
441442
const selector = getBestSelector(element);
442443
ra_len = document.recorded_actions.length;
443-
tag_name = element.tagName.toLowerCase();
444+
tag_name = tagName(element);
444445
e_type = element.type
445446
if (tag_name === 'select')
446447
{
@@ -495,7 +496,7 @@
495496
const element = event.target;
496497
const selector = getBestSelector(element);
497498
ra_len = document.recorded_actions.length;
498-
tag_name = element.tagName.toLowerCase();
499+
tag_name = tagName(element);
499500
if (ra_len > 0 && document.recorded_actions[ra_len-1][0] === 'mo_dn')
500501
document.recorded_actions.pop();
501502
if (tag_name === 'select') {
@@ -513,9 +514,9 @@
513514
const element = event.target;
514515
const selector = getBestSelector(element);
515516
ra_len = document.recorded_actions.length;
516-
tag_name = element.tagName.toLowerCase();
517+
tag_name = tagName(element);
517518
parent_element = element.parentElement;
518-
parent_tag_name = parent_element.tagName.toLowerCase();
519+
parent_tag_name = tagName(parent_element);
519520
grand_element = "";
520521
grand_tag_name = "";
521522
origin = "";
@@ -559,7 +560,7 @@
559560
}
560561
if (parent_element.parentElement != null) {
561562
grand_element = parent_element.parentElement;
562-
grand_tag_name = grand_element.tagName.toLowerCase();
563+
grand_tag_name = tagName(grand_element);
563564
}
564565
if (ra_len > 0 &&
565566
document.recorded_actions[ra_len-1][1] === selector &&
@@ -718,9 +719,9 @@
718719
const element = event.target;
719720
const selector = getBestSelector(element);
720721
skip_input = false;
721-
if ((element.tagName.toLowerCase() === 'input' &&
722+
if ((tagName(element) === 'input' &&
722723
element.type !== 'checkbox' && element.type !== 'range') ||
723-
element.tagName.toLowerCase() === 'textarea')
724+
tagName(element) === 'textarea')
724725
{
725726
ra_len = document.recorded_actions.length;
726727
if (ra_len > 0 && l_key === 'enter' &&

0 commit comments

Comments
 (0)