Skip to content

Commit 9938d6d

Browse files
committed
Refactor browser_launcher and UC Mode
1 parent a232b30 commit 9938d6d

File tree

4 files changed

+109
-28
lines changed

4 files changed

+109
-28
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 98 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -666,11 +666,9 @@ def _set_chrome_options(
666666
chrome_options = webdriver.ChromeOptions()
667667
if is_using_uc(undetectable, browser_name):
668668
from seleniumbase import undetected
669-
670669
chrome_options = undetected.ChromeOptions()
671670
elif browser_name == constants.Browser.EDGE:
672671
chrome_options = webdriver.edge.options.Options()
673-
674672
prefs = {}
675673
prefs["download.default_directory"] = downloads_path
676674
prefs["local_discovery.notifications_enabled"] = False
@@ -2485,12 +2483,12 @@ def get_local_driver(
24852483
binary_location = binary_loc
24862484
if binary_location:
24872485
edge_options.binary_location = binary_location
2486+
service = EdgeService(
2487+
executable_path=LOCAL_EDGEDRIVER,
2488+
log_output=os.devnull,
2489+
service_args=["--disable-build-check"],
2490+
)
24882491
try:
2489-
service = EdgeService(
2490-
executable_path=LOCAL_EDGEDRIVER,
2491-
log_output=os.devnull,
2492-
service_args=["--disable-build-check"],
2493-
)
24942492
driver = Edge(service=service, options=edge_options)
24952493
except Exception as e:
24962494
if not hasattr(e, "msg"):
@@ -2512,11 +2510,6 @@ def get_local_driver(
25122510
"only supports MSEdge version "
25132511
)[1].split(" ")[0]
25142512
elif "DevToolsActivePort file doesn't exist" in e.msg:
2515-
service = EdgeService(
2516-
executable_path=LOCAL_EDGEDRIVER,
2517-
log_output=os.devnull,
2518-
service_args=["--disable-build-check"],
2519-
)
25202513
# https://stackoverflow.com/a/56638103/7058266
25212514
args = " ".join(sys.argv)
25222515
free_port = 9222
@@ -2550,11 +2543,6 @@ def get_local_driver(
25502543
_mark_driver_repaired()
25512544
except Exception:
25522545
pass
2553-
service = EdgeService(
2554-
executable_path=LOCAL_EDGEDRIVER,
2555-
log_output=os.devnull,
2556-
service_args=["--disable-build-check"],
2557-
)
25582546
driver = Edge(service=service, options=edge_options)
25592547
return extend_driver(driver)
25602548
elif browser_name == constants.Browser.SAFARI:
@@ -3019,6 +3007,99 @@ def get_local_driver(
30193007
chrome_options.add_experimental_option(
30203008
"w3c", True
30213009
)
3010+
try:
3011+
if (
3012+
uc_chrome_version
3013+
and uc_chrome_version >= 117
3014+
and (headless or headless2)
3015+
and not user_agent
3016+
):
3017+
headless_options = _set_chrome_options(
3018+
browser_name,
3019+
downloads_path,
3020+
True, # headless
3021+
locale_code,
3022+
proxy_string,
3023+
proxy_auth,
3024+
proxy_user,
3025+
proxy_pass,
3026+
proxy_bypass_list,
3027+
proxy_pac_url,
3028+
multi_proxy,
3029+
user_agent,
3030+
recorder_ext,
3031+
disable_js,
3032+
disable_csp,
3033+
enable_ws,
3034+
enable_sync,
3035+
use_auto_ext,
3036+
False, # Undetectable
3037+
uc_cdp_events,
3038+
uc_subprocess,
3039+
no_sandbox,
3040+
disable_gpu,
3041+
False, # headless2
3042+
incognito,
3043+
guest_mode,
3044+
dark_mode,
3045+
devtools,
3046+
remote_debug,
3047+
enable_3d_apis,
3048+
swiftshader,
3049+
ad_block_on,
3050+
block_images,
3051+
do_not_track,
3052+
chromium_arg,
3053+
user_data_dir,
3054+
extension_zip,
3055+
extension_dir,
3056+
binary_location,
3057+
driver_version,
3058+
page_load_strategy,
3059+
use_wire,
3060+
external_pdf,
3061+
servername,
3062+
mobile_emulator,
3063+
device_width,
3064+
device_height,
3065+
device_pixel_ratio,
3066+
)
3067+
if not path_chromedriver:
3068+
sb_install.main(
3069+
override="chromedriver %s"
3070+
% use_version,
3071+
intel_for_uc=False,
3072+
force_uc=False,
3073+
)
3074+
d_b_c = "--disable-build-check"
3075+
if os.path.exists(LOCAL_CHROMEDRIVER):
3076+
service = ChromeService(
3077+
executable_path=LOCAL_CHROMEDRIVER,
3078+
log_output=os.devnull,
3079+
service_args=[d_b_c],
3080+
)
3081+
driver = webdriver.Chrome(
3082+
service=service,
3083+
options=headless_options,
3084+
)
3085+
else:
3086+
service = ChromeService(
3087+
log_output=os.devnull,
3088+
service_args=[d_b_c],
3089+
)
3090+
driver = webdriver.Chrome(
3091+
service=service,
3092+
options=headless_options,
3093+
)
3094+
user_agent = driver.execute_script(
3095+
"return navigator.userAgent;"
3096+
).replace("Headless", "")
3097+
chrome_options.add_argument(
3098+
"--user-agent=%s" % user_agent
3099+
)
3100+
driver.quit()
3101+
except Exception:
3102+
pass
30223103
try:
30233104
uc_path = None
30243105
if os.path.exists(LOCAL_UC_DRIVER):

seleniumbase/undetected/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def __init__(
132132
try:
133133
if hasattr(options, "_session") and options._session is not None:
134134
# Prevent reuse of options
135-
raise RuntimeError("you cannot reuse the ChromeOptions object")
135+
raise RuntimeError("You cannot reuse the ChromeOptions object")
136136
except AttributeError:
137137
pass
138138
options._session = self
@@ -434,7 +434,7 @@ def start_session(self, capabilities=None):
434434

435435
def quit(self):
436436
try:
437-
logger.debug("Terminating the browser")
437+
logger.debug("Terminating the UC browser")
438438
os.kill(self.browser_pid, 15)
439439
except TimeoutError as e:
440440
logger.debug(e, exc_info=True)
@@ -445,7 +445,7 @@ def quit(self):
445445
self.service.stop()
446446
try:
447447
if self.reactor and isinstance(self.reactor, Reactor):
448-
logger.debug("Shutting down reactor")
448+
logger.debug("Shutting down Reactor")
449449
self.reactor.event.set()
450450
except Exception:
451451
pass
@@ -464,7 +464,7 @@ def quit(self):
464464
except (RuntimeError, OSError, PermissionError) as e:
465465
logger.debug(
466466
"When removing the temp profile, a %s occured: "
467-
"%s\nretrying..."
467+
"%s\nRetrying..."
468468
% (e.__class__.__name__, e)
469469
)
470470
else:
@@ -473,7 +473,7 @@ def quit(self):
473473
)
474474
break
475475
time.sleep(0.1)
476-
# Dereference patcher, so patcher can start cleaning up as well.
476+
# Dereference Patcher so that it can start cleaning up as well.
477477
# This must come last, otherwise it will throw "in use" errors.
478478
self.patcher = None
479479

seleniumbase/undetected/patcher.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def fetch_release_number(self):
122122
if self.version_main:
123123
path += "_%s" % self.version_main
124124
path = path.upper()
125-
logger.debug("getting release number from %s" % path)
125+
logger.debug("Getting release number from %s" % path)
126126
return urlopen(self.url_repo + path).read().decode()
127127

128128
def fetch_package(self):
@@ -133,12 +133,12 @@ def fetch_package(self):
133133
u = "%s/%s/%s" % (
134134
self.url_repo, self.version_full, self.zip_name
135135
)
136-
logger.debug("downloading from %s" % u)
136+
logger.debug("Downloading from %s" % u)
137137
return urlretrieve(u)[0]
138138

139139
def unzip_package(self, fp):
140140
""" :return: path to unpacked executable """
141-
logger.debug("unzipping %s" % fp)
141+
logger.debug("Unzipping %s" % fp)
142142
try:
143143
os.unlink(self.zip_path)
144144
except (FileNotFoundError, OSError):
@@ -285,14 +285,14 @@ def __del__(self):
285285
now = time.monotonic()
286286
if now - t > timeout:
287287
logger.debug(
288-
"could not unlink %s in time (%d seconds)"
288+
"Could not unlink %s in time (%d seconds)"
289289
% (self.executable_path, timeout)
290290
)
291291
break
292292
try:
293293
os.unlink(self.executable_path)
294294
logger.debug(
295-
"successfully unlinked %s"
295+
"Successfully unlinked %s"
296296
% self.executable_path
297297
)
298298
break

seleniumbase/undetected/reactor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ async def listen(self):
8181
if "invalid session id" in str(e):
8282
pass
8383
else:
84-
logger.debug("exception ignored : %s", e)
84+
logger.debug("Exception ignored: %s", e)

0 commit comments

Comments
 (0)