Skip to content

Commit 91fb216

Browse files
committed
Fix a few things on Chrome 142
1 parent ec4be63 commit 91fb216

File tree

4 files changed

+66
-46
lines changed

4 files changed

+66
-46
lines changed

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/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

seleniumbase/undetected/cdp_driver/config.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ def __init__(
7979
if not browser_args:
8080
browser_args = []
8181
if not user_data_dir:
82-
self._user_data_dir = temp_profile_dir()
82+
self.user_data_dir = temp_profile_dir()
83+
self._user_data_dir = self.user_data_dir
8384
self._custom_data_dir = False
8485
else:
8586
self.user_data_dir = user_data_dir
@@ -315,10 +316,13 @@ def find_chrome_executable(return_all=False):
315316
for item in os.environ.get("PATH").split(os.pathsep):
316317
for subitem in (
317318
"google-chrome",
319+
"google-chrome-stable",
320+
"google-chrome-beta",
321+
"google-chrome-dev",
322+
"google-chrome-unstable",
323+
"chrome",
318324
"chromium",
319325
"chromium-browser",
320-
"chrome",
321-
"google-chrome-stable",
322326
):
323327
candidates.append(os.sep.join((item, subitem)))
324328
if "darwin" in sys.platform:
@@ -347,7 +351,11 @@ def find_chrome_executable(return_all=False):
347351
)
348352
rv = []
349353
for candidate in candidates:
350-
if os.path.exists(candidate) and os.access(candidate, os.X_OK):
354+
if (
355+
os.path.exists(candidate)
356+
and os.access(candidate, os.R_OK)
357+
and os.access(candidate, os.X_OK)
358+
):
351359
logger.debug("%s is a valid candidate... " % candidate)
352360
rv.append(candidate)
353361
else:

0 commit comments

Comments
 (0)