Skip to content

Commit f8d844a

Browse files
authored
Merge pull request #4061 from seleniumbase/a-few-fixes-for-chrome-142-and-more
A few fixes for Chrome 142 and more
2 parents ec4be63 + af258f6 commit f8d844a

File tree

6 files changed

+89
-69
lines changed

6 files changed

+89
-69
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__ = "4.44.2"
2+
__version__ = "4.44.3"

seleniumbase/core/browser_launcher.py

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,17 @@ def extend_driver(
293293
)
294294
if hasattr(driver, "proxy"):
295295
driver.set_wire_proxy = DM.set_wire_proxy
296+
completed_loads = []
297+
for ext_dir in sb_config._ext_dirs:
298+
with suppress(Exception):
299+
if ext_dir not in completed_loads:
300+
completed_loads.append(ext_dir)
301+
if not use_uc and os.path.exists(os.path.abspath(ext_dir)):
302+
driver.webextension.install(os.path.abspath(ext_dir))
296303
if proxy_auth:
304+
with suppress(Exception):
305+
if not use_uc and os.path.exists(proxy_helper.PROXY_DIR_PATH):
306+
driver.webextension.install(proxy_helper.PROXY_DIR_PATH)
297307
# Proxy needs a moment to load in Manifest V3
298308
if use_uc:
299309
time.sleep(0.14)
@@ -2087,6 +2097,7 @@ def _add_chrome_proxy_extension(
20872097
"""Implementation of https://stackoverflow.com/a/35293284/7058266
20882098
for https://stackoverflow.com/q/12848327/7058266
20892099
(Run Selenium on a proxy server that requires authentication.)"""
2100+
zip_it = False
20902101
args = " ".join(sys.argv)
20912102
bypass_list = proxy_bypass_list
20922103
if (
@@ -2467,13 +2478,27 @@ def _set_chrome_options(
24672478
extension_zip_list = extension_zip.split(",")
24682479
for extension_zip_item in extension_zip_list:
24692480
abs_path = os.path.abspath(extension_zip_item)
2470-
chrome_options.add_extension(abs_path)
2481+
if os.path.exists(abs_path):
2482+
try:
2483+
abs_path_dir = os.path.join(
2484+
DOWNLOADS_FOLDER, abs_path.split(".")[0]
2485+
)
2486+
_unzip_to_new_folder(abs_path, abs_path_dir)
2487+
chrome_options = add_chrome_ext_dir(
2488+
chrome_options, abs_path_dir
2489+
)
2490+
sb_config._ext_dirs.append(abs_path_dir)
2491+
except Exception:
2492+
with suppress(Exception):
2493+
chrome_options.add_extension(abs_path)
24712494
if extension_dir:
24722495
# load-extension input can be a comma-separated list
24732496
abs_path = (
24742497
",".join(os.path.abspath(p) for p in extension_dir.split(","))
24752498
)
24762499
chrome_options = add_chrome_ext_dir(chrome_options, abs_path)
2500+
for p in extension_dir.split(","):
2501+
sb_config._ext_dirs.append(os.path.abspath(p))
24772502
if (
24782503
page_load_strategy
24792504
and page_load_strategy.lower() in ["eager", "none"]
@@ -2508,37 +2533,32 @@ def _set_chrome_options(
25082533
if (settings.DISABLE_CSP_ON_CHROME or disable_csp) and not headless:
25092534
# Headless Chrome does not support extensions, which are required
25102535
# for disabling the Content Security Policy on Chrome.
2511-
if is_using_uc(undetectable, browser_name):
2512-
disable_csp_zip = DISABLE_CSP_ZIP_PATH
2513-
disable_csp_dir = os.path.join(DOWNLOADS_FOLDER, "disable_csp")
2514-
_unzip_to_new_folder(disable_csp_zip, disable_csp_dir)
2515-
chrome_options = add_chrome_ext_dir(
2516-
chrome_options, disable_csp_dir
2517-
)
2518-
else:
2519-
chrome_options = _add_chrome_disable_csp_extension(chrome_options)
2536+
disable_csp_zip = DISABLE_CSP_ZIP_PATH
2537+
disable_csp_dir = os.path.join(DOWNLOADS_FOLDER, "disable_csp")
2538+
_unzip_to_new_folder(disable_csp_zip, disable_csp_dir)
2539+
chrome_options = add_chrome_ext_dir(
2540+
chrome_options, disable_csp_dir
2541+
)
2542+
sb_config._ext_dirs.append(disable_csp_dir)
25202543
if ad_block_on and not headless:
25212544
# Headless Chrome does not support extensions.
2522-
if is_using_uc(undetectable, browser_name):
2523-
ad_block_zip = AD_BLOCK_ZIP_PATH
2524-
ad_block_dir = os.path.join(DOWNLOADS_FOLDER, "ad_block")
2525-
_unzip_to_new_folder(ad_block_zip, ad_block_dir)
2526-
chrome_options = add_chrome_ext_dir(chrome_options, ad_block_dir)
2527-
else:
2528-
chrome_options = _add_chrome_ad_block_extension(chrome_options)
2545+
ad_block_zip = AD_BLOCK_ZIP_PATH
2546+
ad_block_dir = os.path.join(DOWNLOADS_FOLDER, "ad_block")
2547+
_unzip_to_new_folder(ad_block_zip, ad_block_dir)
2548+
chrome_options = add_chrome_ext_dir(chrome_options, ad_block_dir)
2549+
sb_config._ext_dirs.append(ad_block_dir)
25292550
if recorder_ext and not headless:
2530-
if is_using_uc(undetectable, browser_name):
2531-
recorder_zip = RECORDER_ZIP_PATH
2532-
recorder_dir = os.path.join(DOWNLOADS_FOLDER, "recorder")
2533-
_unzip_to_new_folder(recorder_zip, recorder_dir)
2534-
chrome_options = add_chrome_ext_dir(chrome_options, recorder_dir)
2535-
else:
2536-
chrome_options = _add_chrome_recorder_extension(chrome_options)
2551+
recorder_zip = RECORDER_ZIP_PATH
2552+
recorder_dir = os.path.join(DOWNLOADS_FOLDER, "recorder")
2553+
_unzip_to_new_folder(recorder_zip, recorder_dir)
2554+
chrome_options = add_chrome_ext_dir(chrome_options, recorder_dir)
2555+
sb_config._ext_dirs.append(recorder_dir)
25372556
if chromium_arg and "sbase" in chromium_arg:
25382557
sbase_ext_zip = SBASE_EXT_ZIP_PATH
25392558
sbase_ext_dir = os.path.join(DOWNLOADS_FOLDER, "sbase_ext")
25402559
_unzip_to_new_folder(sbase_ext_zip, sbase_ext_dir)
25412560
chrome_options = add_chrome_ext_dir(chrome_options, sbase_ext_dir)
2561+
sb_config._ext_dirs.append(sbase_ext_dir)
25422562
if proxy_string:
25432563
if proxy_auth:
25442564
zip_it = True
@@ -2724,6 +2744,10 @@ def _set_chrome_options(
27242744
chrome_options.add_argument("--disable-features=%s" % d_f_string)
27252745
if proxy_auth:
27262746
chrome_options.add_argument("--test-type")
2747+
if proxy_auth or sb_config._ext_dirs:
2748+
if not is_using_uc(undetectable, browser_name):
2749+
chrome_options.enable_webextensions = True
2750+
chrome_options.enable_bidi = True
27272751
if (
27282752
is_using_uc(undetectable, browser_name)
27292753
and (
@@ -2988,6 +3012,7 @@ def get_driver(
29883012
device_pixel_ratio=None,
29893013
browser=None, # A duplicate of browser_name to avoid confusion
29903014
):
3015+
sb_config._ext_dirs = []
29913016
driver_dir = DRIVER_DIR
29923017
if (
29933018
hasattr(sb_config, "binary_location")

seleniumbase/fixtures/base_case.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4694,7 +4694,7 @@ def save_cookies(self, name="cookies.txt"):
46944694
if not os.path.exists(file_path):
46954695
os.makedirs(file_path)
46964696
cookies_file_path = os.path.join(file_path, name)
4697-
cookies_file = open(cookies_file_path, "w+", encoding="utf-8")
4697+
cookies_file = open(cookies_file_path, mode="w+", encoding="utf-8")
46984698
cookies_file.writelines(json_cookies)
46994699
cookies_file.close()
47004700

@@ -5751,7 +5751,7 @@ def __process_recorded_actions(self):
57515751
extra_file_name = "__init__.py"
57525752
extra_file_path = os.path.join(recordings_folder, extra_file_name)
57535753
if not os.path.exists(extra_file_path):
5754-
out_file = open(extra_file_path, "w+", "utf-8")
5754+
out_file = open(extra_file_path, mode="w+", encoding="utf-8")
57555755
out_file.writelines("\r\n".join(data))
57565756
out_file.close()
57575757
sys.stdout.write("\nCreated recordings%s__init__.py" % os.sep)
@@ -5799,7 +5799,7 @@ def __process_recorded_actions(self):
57995799
extra_file_name = "pytest.ini"
58005800
extra_file_path = os.path.join(recordings_folder, extra_file_name)
58015801
if not os.path.exists(extra_file_path):
5802-
out_file = open(extra_file_path, "w+", "utf-8")
5802+
out_file = open(extra_file_path, mode="w+", encoding="utf-8")
58035803
out_file.writelines("\r\n".join(data))
58045804
out_file.close()
58055805
sys.stdout.write("\nCreated recordings%spytest.ini" % os.sep)
@@ -5820,7 +5820,7 @@ def __process_recorded_actions(self):
58205820
extra_file_name = "setup.cfg"
58215821
extra_file_path = os.path.join(recordings_folder, extra_file_name)
58225822
if not os.path.exists(extra_file_path):
5823-
out_file = open(extra_file_path, "w+", "utf-8")
5823+
out_file = open(extra_file_path, mode="w+", encoding="utf-8")
58245824
out_file.writelines("\r\n".join(data))
58255825
out_file.close()
58265826
sys.stdout.write("\nCreated recordings%ssetup.cfg" % os.sep)
@@ -5838,7 +5838,7 @@ def __process_recorded_actions(self):
58385838
elif context_filename:
58395839
file_name = context_filename
58405840
file_path = os.path.join(recordings_folder, file_name)
5841-
out_file = open(file_path, "w+", "utf-8")
5841+
out_file = open(file_path, mode="w+", encoding="utf-8")
58425842
out_file.writelines("\r\n".join(data))
58435843
out_file.close()
58445844
rec_message = ">>> RECORDING SAVED as: "
@@ -5940,7 +5940,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
59405940
file_name = sb_config.behave_scenario.filename.replace(".", "_")
59415941
file_name = file_name.split("/")[-1].split("\\")[-1] + "_rec.feature"
59425942
file_path = os.path.join(features_folder, file_name)
5943-
out_file = open(file_path, "w+", "utf-8")
5943+
out_file = open(file_path, mode="w+", encoding="utf-8")
59445944
out_file.writelines("\r\n".join(data))
59455945
out_file.close()
59465946

@@ -5978,7 +5978,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
59785978
file_name = "__init__.py"
59795979
file_path = os.path.join(features_folder, file_name)
59805980
if not os.path.exists(file_path):
5981-
out_file = open(file_path, "w+", "utf-8")
5981+
out_file = open(file_path, mode="w+", encoding="utf-8")
59825982
out_file.writelines("\r\n".join(data))
59835983
out_file.close()
59845984
print("Created recordings/features/__init__.py")
@@ -5991,7 +5991,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
59915991
file_name = "behave.ini"
59925992
file_path = os.path.join(features_folder, file_name)
59935993
if not os.path.exists(file_path):
5994-
out_file = open(file_path, "w+", "utf-8")
5994+
out_file = open(file_path, mode="w+", encoding="utf-8")
59955995
out_file.writelines("\r\n".join(data))
59965996
out_file.close()
59975997
print("Created recordings/features/behave.ini")
@@ -6030,7 +6030,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
60306030
file_name = "environment.py"
60316031
file_path = os.path.join(features_folder, file_name)
60326032
if not os.path.exists(file_path):
6033-
out_file = open(file_path, "w+", "utf-8")
6033+
out_file = open(file_path, mode="w+", encoding="utf-8")
60346034
out_file.writelines("\r\n".join(data))
60356035
out_file.close()
60366036
print("Created recordings/features/environment.py")
@@ -6040,7 +6040,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
60406040
file_name = "__init__.py"
60416041
file_path = os.path.join(steps_folder, file_name)
60426042
if not os.path.exists(file_path):
6043-
out_file = open(file_path, "w+", "utf-8")
6043+
out_file = open(file_path, mode="w+", encoding="utf-8")
60446044
out_file.writelines("\r\n".join(data))
60456045
out_file.close()
60466046
print("Created recordings/features/steps/__init__.py")
@@ -6051,7 +6051,7 @@ def __process_recorded_behave_actions(self, srt_actions, colorama):
60516051
file_name = "imported.py"
60526052
file_path = os.path.join(steps_folder, file_name)
60536053
if not os.path.exists(file_path):
6054-
out_file = open(file_path, "w+", "utf-8")
6054+
out_file = open(file_path, mode="w+", encoding="utf-8")
60556055
out_file.writelines("\r\n".join(data))
60566056
out_file.close()
60576057
print("Created recordings/features/steps/imported.py")
@@ -11120,7 +11120,7 @@ def __process_visual_baseline_logs(self):
1112011120
return # Skip the rest when deferred visual asserts are used
1112111121
the_html = visual_helper.get_sbs_html()
1112211122
file_path = os.path.join(test_logpath, constants.SideBySide.HTML_FILE)
11123-
out_file = open(file_path, "w+", encoding="utf-8")
11123+
out_file = open(file_path, mode="w+", encoding="utf-8")
1112411124
out_file.writelines(the_html)
1112511125
out_file.close()
1112611126

@@ -11280,16 +11280,16 @@ def check_window(
1128011280
self.save_screenshot(
1128111281
baseline_png, visual_baseline_path, selector="body"
1128211282
)
11283-
out_file = open(page_url_file, "w+", encoding="utf-8")
11283+
out_file = open(page_url_file, mode="w+", encoding="utf-8")
1128411284
out_file.writelines(page_url)
1128511285
out_file.close()
11286-
out_file = open(level_1_file, "w+", encoding="utf-8")
11286+
out_file = open(level_1_file, mode="w+", encoding="utf-8")
1128711287
out_file.writelines(json.dumps(level_1))
1128811288
out_file.close()
11289-
out_file = open(level_2_file, "w+", encoding="utf-8")
11289+
out_file = open(level_2_file, mode="w+", encoding="utf-8")
1129011290
out_file.writelines(json.dumps(level_2))
1129111291
out_file.close()
11292-
out_file = open(level_3_file, "w+", encoding="utf-8")
11292+
out_file = open(level_3_file, mode="w+", encoding="utf-8")
1129311293
out_file.writelines(json.dumps(level_3))
1129411294
out_file.close()
1129511295

@@ -11428,7 +11428,7 @@ def check_window(
1142811428
alpha_n_d_name = "".join([x if x.isalnum() else "_" for x in name])
1142911429
side_by_side_name = "side_by_side_%s.html" % alpha_n_d_name
1143011430
file_path = os.path.join(test_logpath, side_by_side_name)
11431-
out_file = open(file_path, "w+", encoding="utf-8")
11431+
out_file = open(file_path, mode="w+", encoding="utf-8")
1143211432
out_file.writelines(the_html)
1143311433
out_file.close()
1143411434

@@ -12120,7 +12120,7 @@ def save_presentation(
1212012120
with suppress(Exception):
1212112121
os.makedirs(saved_presentations_folder)
1212212122
file_path = os.path.join(saved_presentations_folder, filename)
12123-
out_file = open(file_path, "w+", encoding="utf-8")
12123+
out_file = open(file_path, mode="w+", encoding="utf-8")
1212412124
out_file.writelines(the_html)
1212512125
out_file.close()
1212612126
if self._output_file_saves:
@@ -12815,7 +12815,7 @@ def save_chart(self, chart_name=None, filename=None, folder=None):
1281512815
with suppress(Exception):
1281612816
os.makedirs(saved_charts_folder)
1281712817
file_path = os.path.join(saved_charts_folder, filename)
12818-
out_file = open(file_path, "w+", encoding="utf-8")
12818+
out_file = open(file_path, mode="w+", encoding="utf-8")
1281912819
out_file.writelines(the_html)
1282012820
out_file.close()
1282112821
if self._output_file_saves:
@@ -16378,7 +16378,7 @@ def __process_dashboard(self, has_exception, init=False):
1637816378
dash_pie = json.dumps(sb_config._saved_dashboard_pie)
1637916379
dash_pie_loc = constants.Dashboard.DASH_PIE
1638016380
pie_path = os.path.join(abs_path, dash_pie_loc)
16381-
pie_file = open(pie_path, "w+", encoding="utf-8")
16381+
pie_file = open(pie_path, mode="w+", encoding="utf-8")
1638216382
pie_file.writelines(dash_pie)
1638316383
pie_file.close()
1638416384
DASH_PIE_PNG_1 = constants.Dashboard.get_dash_pie_1()
@@ -16538,7 +16538,7 @@ def __process_dashboard(self, has_exception, init=False):
1653816538
)
1653916539
abs_path = os.path.abspath(".")
1654016540
file_path = os.path.join(abs_path, "dashboard.html")
16541-
out_file = open(file_path, "w+", encoding="utf-8")
16541+
out_file = open(file_path, mode="w+", encoding="utf-8")
1654216542
out_file.writelines(the_html)
1654316543
out_file.close()
1654416544
sb_config._dash_html = the_html
@@ -16551,7 +16551,7 @@ def __process_dashboard(self, has_exception, init=False):
1655116551
dash_json = json.dumps((_results, _display_id, _rt, _tlp, d_stats))
1655216552
dash_json_loc = constants.Dashboard.DASH_JSON
1655316553
dash_jsonpath = os.path.join(abs_path, dash_json_loc)
16554-
dash_json_file = open(dash_jsonpath, "w+", encoding="utf-8")
16554+
dash_json_file = open(dash_jsonpath, mode="w+", encoding="utf-8")
1655516555
dash_json_file.writelines(dash_json)
1655616556
dash_json_file.close()
1655716557

seleniumbase/undetected/cdp.py

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import fasteners
21
import json
32
import logging
43
import requests
5-
from seleniumbase.fixtures import constants
6-
from seleniumbase.fixtures import shared_utils
4+
import websockets
75

86
log = logging.getLogger(__name__)
97

@@ -107,15 +105,6 @@ def tab_close_last_opened(self):
107105
return resp.json()
108106

109107
async def send(self, method, params):
110-
pip_find_lock = fasteners.InterProcessLock(
111-
constants.PipInstall.FINDLOCK
112-
)
113-
with pip_find_lock:
114-
try:
115-
import websockets
116-
except Exception:
117-
shared_utils.pip_install("websockets")
118-
import websockets
119108
self._reqid += 1
120109
async with websockets.connect(self.wsurl) as ws:
121110
await ws.send(

seleniumbase/undetected/cdp_driver/browser.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,7 @@ async def get(
350350
if _cdp_geolocation:
351351
await connection.send(cdp.page.navigate("about:blank"))
352352
await connection.set_geolocation(_cdp_geolocation)
353-
# This part isn't needed now, but may be needed later
354-
"""
353+
# (The code below is for the Chrome 142 extension fix)
355354
if (
356355
hasattr(sb_config, "_cdp_proxy")
357356
and "@" in sb_config._cdp_proxy
@@ -363,7 +362,6 @@ async def get(
363362
proxy_pass = username_and_password.split(":")[1]
364363
await connection.set_auth(proxy_user, proxy_pass, self.tabs[0])
365364
time.sleep(0.25)
366-
"""
367365
if "auth" in kwargs and kwargs["auth"] and ":" in kwargs["auth"]:
368366
username_and_password = kwargs["auth"]
369367
proxy_user = username_and_password.split(":")[0]
@@ -375,12 +373,12 @@ async def get(
375373
cdp.page.navigate(url)
376374
)
377375
if _cdp_recorder:
378-
pass # (The code below was for the Chrome 137 extension fix)
379-
'''from seleniumbase.js_code.recorder_js import recorder_js
376+
# (The code below is for the Chrome 142 extension fix)
377+
from seleniumbase.js_code.recorder_js import recorder_js
380378
recorder_code = (
381379
"""window.onload = function() { %s };""" % recorder_js
382380
)
383-
await connection.send(cdp.runtime.evaluate(recorder_code))'''
381+
await connection.send(cdp.runtime.evaluate(recorder_code))
384382
# Update the frame_id on the tab
385383
connection.frame_id = frame_id
386384
connection.browser = self

0 commit comments

Comments
 (0)