Skip to content

Commit 86273f4

Browse files
committed
Make general improvements to UC Mode
1 parent 5d6a816 commit 86273f4

File tree

5 files changed

+93
-56
lines changed

5 files changed

+93
-56
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2523,7 +2523,7 @@ def get_local_driver(
25232523
chrome_options.add_argument("--headless=new")
25242524
disable_build_check = False
25252525
uc_driver_version = None
2526-
if IS_ARM_MAC:
2526+
if is_using_uc(undetectable, browser_name):
25272527
uc_driver_version = get_uc_driver_version()
25282528
if (
25292529
LOCAL_CHROMEDRIVER
@@ -2609,22 +2609,39 @@ def get_local_driver(
26092609
)
26102610
)
26112611
):
2612-
sb_install.main(
2613-
override="chromedriver latest"
2614-
)
2612+
sb_install.main(override="chromedriver latest")
26152613
sys.argv = sys_args # Put back the original sys args
26162614
else:
2615+
# (Multithreaded)
26172616
chromedriver_fixing_lock = fasteners.InterProcessLock(
26182617
constants.MultiBrowser.DRIVER_FIXING_LOCK
26192618
)
26202619
with chromedriver_fixing_lock:
2621-
if not chromedriver_on_path():
2622-
sys_args = sys.argv # Save a copy of sys args
2620+
msg = "chromedriver update needed. Getting it now:"
2621+
if not path_chromedriver:
26232622
msg = "chromedriver not found. Getting it now:"
2623+
intel_for_uc = False
2624+
if (
2625+
IS_ARM_MAC
2626+
and is_using_uc(undetectable, browser_name)
2627+
):
2628+
intel_for_uc = True # Use Intel driver for UC Mode
2629+
if (
2630+
(
2631+
not is_using_uc(undetectable, browser_name)
2632+
and not os.path.exists(LOCAL_CHROMEDRIVER)
2633+
)
2634+
or (
2635+
is_using_uc(undetectable, browser_name)
2636+
and not os.path.exists(LOCAL_UC_DRIVER)
2637+
)
2638+
):
26242639
print("\nWarning: %s" % msg)
2640+
sys_args = sys.argv # Save a copy of sys args
26252641
try:
26262642
sb_install.main(
2627-
override="chromedriver %s" % use_version
2643+
override="chromedriver %s" % use_version,
2644+
intel_for_uc=intel_for_uc,
26282645
)
26292646
except Exception:
26302647
d_latest = get_latest_chromedriver_version()
@@ -2651,7 +2668,8 @@ def get_local_driver(
26512668
sb_install.main(
26522669
override="chromedriver latest"
26532670
)
2654-
sys.argv = sys_args # Put back original sys args
2671+
finally:
2672+
sys.argv = sys_args # Put back original args
26552673
service_args = []
26562674
if disable_build_check:
26572675
service_args = ["--disable-build-check"]
@@ -2662,10 +2680,13 @@ def get_local_driver(
26622680
with uc_lock: # Avoid multithreaded issues
26632681
uc_driver_version = get_uc_driver_version()
26642682
if (
2665-
uc_driver_version != use_version
2666-
and use_version != "latest"
2683+
(
2684+
uc_driver_version != use_version
2685+
and use_version != "latest"
2686+
)
2687+
or not os.path.exists(LOCAL_UC_DRIVER)
26672688
):
2668-
if IS_ARM_MAC:
2689+
if IS_ARM_MAC and os.path.exists(LOCAL_UC_DRIVER):
26692690
pass # Already taken care of in sb_install
26702691
elif os.path.exists(LOCAL_CHROMEDRIVER):
26712692
shutil.copyfile(
@@ -2706,53 +2727,62 @@ def get_local_driver(
27062727
chrome_options.arguments.remove(
27072728
"--headless"
27082729
)
2709-
cert = "unable to get local issuer certificate"
27102730
uc_chrome_version = None
27112731
if (
27122732
use_version.isnumeric()
27132733
and int(use_version) >= 72
27142734
):
27152735
uc_chrome_version = int(use_version)
2716-
uc_lock = fasteners.InterProcessLock(
2717-
constants.MultiBrowser.DRIVER_FIXING_LOCK
2718-
)
2719-
with uc_lock: # Avoid multithreaded issues
2720-
cdp_events = uc_cdp_events
2721-
try:
2722-
uc_path = None
2723-
if os.path.exists(LOCAL_UC_DRIVER):
2724-
uc_path = LOCAL_UC_DRIVER
2725-
uc_path = os.path.realpath(uc_path)
2726-
driver = undetected.Chrome(
2727-
options=chrome_options,
2728-
user_data_dir=user_data_dir,
2729-
driver_executable_path=uc_path,
2730-
enable_cdp_events=cdp_events,
2731-
headless=False, # Xvfb needed!
2732-
version_main=uc_chrome_version,
2733-
use_subprocess=True, # Always!
2734-
)
2735-
except URLError as e:
2736-
if (
2737-
cert in e.args[0]
2738-
and "darwin" in PLATFORM
2739-
):
2736+
cdp_events = uc_cdp_events
2737+
cert = "unable to get local issuer certificate"
2738+
mac_certificate_error = False
2739+
try:
2740+
uc_path = None
2741+
if os.path.exists(LOCAL_UC_DRIVER):
2742+
uc_path = LOCAL_UC_DRIVER
2743+
uc_path = os.path.realpath(uc_path)
2744+
driver = undetected.Chrome(
2745+
options=chrome_options,
2746+
user_data_dir=user_data_dir,
2747+
driver_executable_path=uc_path,
2748+
enable_cdp_events=cdp_events,
2749+
headless=False, # Xvfb needed!
2750+
version_main=uc_chrome_version,
2751+
use_subprocess=True, # Always!
2752+
)
2753+
except URLError as e:
2754+
if (
2755+
cert in e.args[0]
2756+
and "darwin" in PLATFORM
2757+
):
2758+
mac_certificate_error = True
2759+
else:
2760+
raise
2761+
if mac_certificate_error:
2762+
cf_lock_path = (
2763+
constants.MultiBrowser.CERT_FIXING_LOCK
2764+
)
2765+
cf_lock = fasteners.InterProcessLock(
2766+
constants.MultiBrowser.CERT_FIXING_LOCK
2767+
)
2768+
if not os.path.exists(cf_lock_path):
2769+
# Avoid multithreaded issues
2770+
with cf_lock:
2771+
# Install Python Certificates (MAC)
27402772
os.system(
27412773
r"bash /Applications/Python*/"
27422774
r"Install\ "
27432775
r"Certificates.command"
27442776
)
2745-
driver = undetected.Chrome(
2746-
options=chrome_options,
2747-
user_data_dir=user_data_dir,
2748-
driver_executable_path=uc_path,
2749-
enable_cdp_events=cdp_events,
2750-
headless=False, # Xvfb needed!
2751-
version_main=uc_chrome_version,
2752-
use_subprocess=True, # Always!
2753-
)
2754-
else:
2755-
raise
2777+
driver = undetected.Chrome(
2778+
options=chrome_options,
2779+
user_data_dir=user_data_dir,
2780+
driver_executable_path=uc_path,
2781+
enable_cdp_events=cdp_events,
2782+
headless=False, # Xvfb needed!
2783+
version_main=uc_chrome_version,
2784+
use_subprocess=True, # Always!
2785+
)
27562786
else:
27572787
service = ChromeService(
27582788
executable_path=LOCAL_CHROMEDRIVER,

seleniumbase/fixtures/base_case.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2610,6 +2610,7 @@ def __element_click(self, element):
26102610
or self.uc_cdp_events
26112611
or self.__uc_frame_layer > 0
26122612
or not hasattr(element, "uc_click")
2613+
or element.tag_name.lower() != "a"
26132614
):
26142615
element.click()
26152616
else:

seleniumbase/fixtures/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def get_favicon():
123123
class MultiBrowser:
124124
DRIVER_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/driver_fixing.lock"
125125
DRIVER_REPAIRED = Files.DOWNLOADS_FOLDER + "/driver_fixed.lock"
126+
CERT_FIXING_LOCK = Files.DOWNLOADS_FOLDER + "/cert_fixing.lock"
126127

127128

128129
class SavedCookies:

seleniumbase/undetected/__init__.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,20 @@ def __init__(
126126
self.debug = debug
127127
self.patcher = None
128128
if patch_driver:
129-
patcher = Patcher(
130-
executable_path=driver_executable_path,
131-
force=patcher_force_close,
132-
version_main=version_main,
129+
import fasteners
130+
from seleniumbase.fixtures import constants
131+
132+
uc_lock = fasteners.InterProcessLock(
133+
constants.MultiBrowser.DRIVER_FIXING_LOCK
133134
)
134-
patcher.auto()
135-
self.patcher = patcher
135+
with uc_lock:
136+
patcher = Patcher(
137+
executable_path=driver_executable_path,
138+
force=patcher_force_close,
139+
version_main=version_main,
140+
)
141+
patcher.auto()
142+
self.patcher = patcher
136143
if not options:
137144
options = ChromeOptions()
138145
try:

seleniumbase/undetected/webelement.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
class WebElement(selenium.webdriver.remote.webelement.WebElement):
66
def uc_click(self):
77
super().click()
8-
if hasattr(settings, "SKIP_JS_WAITS") and settings.SKIP_JS_WAITS:
9-
pass
10-
elif (
8+
if (
119
hasattr(settings, "PAGE_LOAD_STRATEGY")
1210
and settings.PAGE_LOAD_STRATEGY == "none"
1311
):

0 commit comments

Comments
 (0)