Skip to content

Commit 7b68a9d

Browse files
committed
Make improvements to CDP Mode
1 parent decb00d commit 7b68a9d

File tree

5 files changed

+96
-72
lines changed

5 files changed

+96
-72
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import urllib3
1313
import warnings
1414
from contextlib import suppress
15+
from filelock import FileLock
1516
from selenium import webdriver
1617
from selenium.common.exceptions import ElementClickInterceptedException
1718
from selenium.common.exceptions import InvalidSessionIdException
@@ -429,6 +430,12 @@ def __is_cdp_swap_needed(driver):
429430
return shared_utils.is_cdp_swap_needed(driver)
430431

431432

433+
def uc_execute_cdp_cmd(driver, *args, **kwargs):
434+
if not driver.is_connected():
435+
driver.connect()
436+
return driver.default_execute_cdp_cmd(*args, **kwargs)
437+
438+
432439
def uc_special_open_if_cf(
433440
driver,
434441
url,
@@ -641,7 +648,7 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
641648
)
642649
loop.run_until_complete(driver.cdp_base.wait(0))
643650

644-
gui_lock = fasteners.InterProcessLock(constants.MultiBrowser.PYAUTOGUILOCK)
651+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
645652

646653
if (
647654
"chrome-extension://" in str(driver.cdp_base.main_tab)
@@ -1080,9 +1087,7 @@ def uc_gui_press_key(driver, key):
10801087
install_pyautogui_if_missing(driver)
10811088
import pyautogui
10821089
pyautogui = get_configured_pyautogui(pyautogui)
1083-
gui_lock = fasteners.InterProcessLock(
1084-
constants.MultiBrowser.PYAUTOGUILOCK
1085-
)
1090+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
10861091
with gui_lock:
10871092
pyautogui.press(key)
10881093

@@ -1091,9 +1096,7 @@ def uc_gui_press_keys(driver, keys):
10911096
install_pyautogui_if_missing(driver)
10921097
import pyautogui
10931098
pyautogui = get_configured_pyautogui(pyautogui)
1094-
gui_lock = fasteners.InterProcessLock(
1095-
constants.MultiBrowser.PYAUTOGUILOCK
1096-
)
1099+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
10971100
with gui_lock:
10981101
for key in keys:
10991102
pyautogui.press(key)
@@ -1103,9 +1106,7 @@ def uc_gui_write(driver, text):
11031106
install_pyautogui_if_missing(driver)
11041107
import pyautogui
11051108
pyautogui = get_configured_pyautogui(pyautogui)
1106-
gui_lock = fasteners.InterProcessLock(
1107-
constants.MultiBrowser.PYAUTOGUILOCK
1108-
)
1109+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
11091110
with gui_lock:
11101111
pyautogui.write(text)
11111112

@@ -1138,9 +1139,7 @@ def _uc_gui_click_x_y(driver, x, y, timeframe=0.25, uc_lock=False):
11381139
% (x, y, screen_width, screen_height)
11391140
)
11401141
if uc_lock:
1141-
gui_lock = fasteners.InterProcessLock(
1142-
constants.MultiBrowser.PYAUTOGUILOCK
1143-
)
1142+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
11441143
with gui_lock: # Prevent issues with multiple processes
11451144
pyautogui.moveTo(x, y, timeframe, pyautogui.easeOutQuad)
11461145
if timeframe >= 0.25:
@@ -1159,9 +1158,7 @@ def _uc_gui_click_x_y(driver, x, y, timeframe=0.25, uc_lock=False):
11591158

11601159

11611160
def uc_gui_click_x_y(driver, x, y, timeframe=0.25):
1162-
gui_lock = fasteners.InterProcessLock(
1163-
constants.MultiBrowser.PYAUTOGUILOCK
1164-
)
1161+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
11651162
with gui_lock: # Prevent issues with multiple processes
11661163
install_pyautogui_if_missing(driver)
11671164
import pyautogui
@@ -1280,9 +1277,7 @@ def _uc_gui_click_captcha(
12801277
x = None
12811278
y = None
12821279
visible_iframe = True
1283-
gui_lock = fasteners.InterProcessLock(
1284-
constants.MultiBrowser.PYAUTOGUILOCK
1285-
)
1280+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
12861281
with gui_lock: # Prevent issues with multiple processes
12871282
needs_switch = False
12881283
width_ratio = 1.0
@@ -1643,9 +1638,7 @@ def _uc_gui_handle_captcha_(driver, frame="iframe", ctype=None):
16431638
import pyautogui
16441639
pyautogui = get_configured_pyautogui(pyautogui)
16451640
visible_iframe = True
1646-
gui_lock = fasteners.InterProcessLock(
1647-
constants.MultiBrowser.PYAUTOGUILOCK
1648-
)
1641+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
16491642
with gui_lock: # Prevent issues with multiple processes
16501643
needs_switch = False
16511644
if not __is_cdp_swap_needed(driver):
@@ -5691,6 +5684,12 @@ def get_local_driver(
56915684
driver, *args, **kwargs
56925685
)
56935686
)
5687+
driver.default_execute_cdp_cmd = driver.execute_cdp_cmd
5688+
driver.execute_cdp_cmd = (
5689+
lambda *args, **kwargs: uc_execute_cdp_cmd(
5690+
driver, *args, **kwargs
5691+
)
5692+
)
56945693
driver._is_hidden = (headless or headless2)
56955694
driver._is_using_uc = True
56965695
with suppress(Exception):

seleniumbase/core/sb_cdp.py

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import sys
77
import time
88
from contextlib import suppress
9+
from filelock import FileLock
910
from seleniumbase import config as sb_config
1011
from seleniumbase.config import settings
1112
from seleniumbase.fixtures import constants
@@ -1065,24 +1066,42 @@ def medimize(self):
10651066
time.sleep(0.044)
10661067
return self.loop.run_until_complete(self.page.medimize())
10671068

1068-
def set_window_rect(self, x, y, width, height):
1069-
if self.get_window()[1].window_state.value == "minimized":
1070-
self.loop.run_until_complete(
1069+
def __set_window_rect(self, x, y, width, height, uc_lock=False):
1070+
if uc_lock:
1071+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
1072+
with gui_lock:
1073+
self.__make_sure_pyautogui_lock_is_writable()
1074+
if self.get_window()[1].window_state.value == "minimized":
1075+
self.loop.run_until_complete(
1076+
self.page.set_window_size(
1077+
left=x, top=y, width=width, height=height)
1078+
)
1079+
time.sleep(0.044)
1080+
return self.loop.run_until_complete(
1081+
self.page.set_window_size(
1082+
left=x, top=y, width=width, height=height)
1083+
)
1084+
else:
1085+
if self.get_window()[1].window_state.value == "minimized":
1086+
self.loop.run_until_complete(
1087+
self.page.set_window_size(
1088+
left=x, top=y, width=width, height=height)
1089+
)
1090+
time.sleep(0.044)
1091+
return self.loop.run_until_complete(
10711092
self.page.set_window_size(
10721093
left=x, top=y, width=width, height=height)
10731094
)
1074-
time.sleep(0.044)
1075-
return self.loop.run_until_complete(
1076-
self.page.set_window_size(
1077-
left=x, top=y, width=width, height=height)
1078-
)
1095+
1096+
def set_window_rect(self, x, y, width, height):
1097+
return self.__set_window_rect(x, y, width, height, uc_lock=True)
10791098

10801099
def reset_window_size(self):
10811100
x = settings.WINDOW_START_X
10821101
y = settings.WINDOW_START_Y
10831102
width = settings.CHROME_START_WIDTH
10841103
height = settings.CHROME_START_HEIGHT
1085-
self.set_window_rect(x, y, width, height)
1104+
self.__set_window_rect(x, y, width, height, uc_lock=True)
10861105
self.__add_light_pause()
10871106

10881107
def open_new_window(self, url=None, switch_to=True):
@@ -1548,9 +1567,7 @@ def gui_press_key(self, key):
15481567
self.__install_pyautogui_if_missing()
15491568
import pyautogui
15501569
pyautogui = self.__get_configured_pyautogui(pyautogui)
1551-
gui_lock = fasteners.InterProcessLock(
1552-
constants.MultiBrowser.PYAUTOGUILOCK
1553-
)
1570+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
15541571
with gui_lock:
15551572
self.__make_sure_pyautogui_lock_is_writable()
15561573
pyautogui.press(key)
@@ -1562,9 +1579,7 @@ def gui_press_keys(self, keys):
15621579
self.__install_pyautogui_if_missing()
15631580
import pyautogui
15641581
pyautogui = self.__get_configured_pyautogui(pyautogui)
1565-
gui_lock = fasteners.InterProcessLock(
1566-
constants.MultiBrowser.PYAUTOGUILOCK
1567-
)
1582+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
15681583
with gui_lock:
15691584
self.__make_sure_pyautogui_lock_is_writable()
15701585
for key in keys:
@@ -1577,9 +1592,7 @@ def gui_write(self, text):
15771592
self.__install_pyautogui_if_missing()
15781593
import pyautogui
15791594
pyautogui = self.__get_configured_pyautogui(pyautogui)
1580-
gui_lock = fasteners.InterProcessLock(
1581-
constants.MultiBrowser.PYAUTOGUILOCK
1582-
)
1595+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
15831596
with gui_lock:
15841597
self.__make_sure_pyautogui_lock_is_writable()
15851598
pyautogui.write(text)
@@ -1598,9 +1611,7 @@ def __gui_click_x_y(self, x, y, timeframe=0.25, uc_lock=False):
15981611
% (x, y, screen_width, screen_height)
15991612
)
16001613
if uc_lock:
1601-
gui_lock = fasteners.InterProcessLock(
1602-
constants.MultiBrowser.PYAUTOGUILOCK
1603-
)
1614+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
16041615
with gui_lock: # Prevent issues with multiple processes
16051616
self.__make_sure_pyautogui_lock_is_writable()
16061617
pyautogui.moveTo(x, y, timeframe, pyautogui.easeOutQuad)
@@ -1619,9 +1630,7 @@ def __gui_click_x_y(self, x, y, timeframe=0.25, uc_lock=False):
16191630
pyautogui.click(x=x, y=y)
16201631

16211632
def gui_click_x_y(self, x, y, timeframe=0.25):
1622-
gui_lock = fasteners.InterProcessLock(
1623-
constants.MultiBrowser.PYAUTOGUILOCK
1624-
)
1633+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
16251634
with gui_lock: # Prevent issues with multiple processes
16261635
self.__make_sure_pyautogui_lock_is_writable()
16271636
self.__install_pyautogui_if_missing()
@@ -1645,7 +1654,7 @@ def gui_click_x_y(self, x, y, timeframe=0.25):
16451654
sb_config._saved_width_ratio = width_ratio
16461655
self.minimize()
16471656
self.__add_light_pause()
1648-
self.set_window_rect(win_x, win_y, width, height)
1657+
self.__set_window_rect(win_x, win_y, width, height)
16491658
self.__add_light_pause()
16501659
x = x * width_ratio
16511660
y = y * width_ratio
@@ -1831,9 +1840,7 @@ def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
18311840
% (x2, y2, screen_width, screen_height)
18321841
)
18331842
if uc_lock:
1834-
gui_lock = fasteners.InterProcessLock(
1835-
constants.MultiBrowser.PYAUTOGUILOCK
1836-
)
1843+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
18371844
with gui_lock: # Prevent issues with multiple processes
18381845
pyautogui.moveTo(x1, y1, 0.25, pyautogui.easeOutQuad)
18391846
self.__add_light_pause()
@@ -1851,9 +1858,7 @@ def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
18511858
def gui_drag_drop_points(self, x1, y1, x2, y2, timeframe=0.35):
18521859
"""Use PyAutoGUI to drag-and-drop from one point to another.
18531860
Can simulate click-and-hold when using the same point twice."""
1854-
gui_lock = fasteners.InterProcessLock(
1855-
constants.MultiBrowser.PYAUTOGUILOCK
1856-
)
1861+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
18571862
with gui_lock: # Prevent issues with multiple processes
18581863
self.__install_pyautogui_if_missing()
18591864
import pyautogui
@@ -1876,7 +1881,7 @@ def gui_drag_drop_points(self, x1, y1, x2, y2, timeframe=0.35):
18761881
sb_config._saved_width_ratio = width_ratio
18771882
self.minimize()
18781883
self.__add_light_pause()
1879-
self.set_window_rect(win_x, win_y, width, height)
1884+
self.__set_window_rect(win_x, win_y, width, height)
18801885
self.__add_light_pause()
18811886
x1 = x1 * width_ratio
18821887
y1 = y1 * (width_ratio - 0.02)
@@ -1920,9 +1925,7 @@ def __gui_hover_x_y(self, x, y, timeframe=0.25, uc_lock=False):
19201925
% (x, y, screen_width, screen_height)
19211926
)
19221927
if uc_lock:
1923-
gui_lock = fasteners.InterProcessLock(
1924-
constants.MultiBrowser.PYAUTOGUILOCK
1925-
)
1928+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
19261929
with gui_lock: # Prevent issues with multiple processes
19271930
pyautogui.moveTo(x, y, timeframe, pyautogui.easeOutQuad)
19281931
time.sleep(0.056)
@@ -1936,9 +1939,7 @@ def __gui_hover_x_y(self, x, y, timeframe=0.25, uc_lock=False):
19361939
print(" <DEBUG> pyautogui.moveTo(%s, %s)" % (x, y))
19371940

19381941
def gui_hover_x_y(self, x, y, timeframe=0.25):
1939-
gui_lock = fasteners.InterProcessLock(
1940-
constants.MultiBrowser.PYAUTOGUILOCK
1941-
)
1942+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
19421943
with gui_lock: # Prevent issues with multiple processes
19431944
self.__install_pyautogui_if_missing()
19441945
import pyautogui
@@ -1971,7 +1972,7 @@ def gui_hover_x_y(self, x, y, timeframe=0.25):
19711972
if width_ratio < 0.45 or width_ratio > 2.55:
19721973
width_ratio = 1.01
19731974
sb_config._saved_width_ratio = width_ratio
1974-
self.set_window_rect(win_x, win_y, width, height)
1975+
self.__set_window_rect(win_x, win_y, width, height)
19751976
self.__add_light_pause()
19761977
self.bring_active_window_to_front()
19771978
elif (
@@ -2002,9 +2003,7 @@ def gui_hover_element(self, selector, timeframe=0.25):
20022003
self.loop.run_until_complete(self.page.wait())
20032004

20042005
def gui_hover_and_click(self, hover_selector, click_selector):
2005-
gui_lock = fasteners.InterProcessLock(
2006-
constants.MultiBrowser.PYAUTOGUILOCK
2007-
)
2006+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
20082007
with gui_lock:
20092008
self.__make_sure_pyautogui_lock_is_writable()
20102009
self.bring_active_window_to_front()
@@ -2586,7 +2585,7 @@ class Chrome(CDPMethods):
25862585
def __init__(self, url=None, **kwargs):
25872586
if not url:
25882587
url = "about:blank"
2589-
loop = asyncio.new_event_loop()
25902588
driver = cdp_util.start_sync(**kwargs)
2589+
loop = asyncio.new_event_loop()
25912590
page = loop.run_until_complete(driver.get(url))
25922591
super().__init__(loop, page, driver)

seleniumbase/fixtures/page_actions.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
By.PARTIAL_LINK_TEXT # "partial link text"
1919
"""
2020
import codecs
21-
import fasteners
2221
import os
2322
import time
2423
from contextlib import suppress
24+
from filelock import FileLock
2525
from selenium.common.exceptions import ElementNotInteractableException
2626
from selenium.common.exceptions import ElementNotVisibleException
2727
from selenium.common.exceptions import NoAlertPresentException
@@ -1632,9 +1632,7 @@ def __switch_to_window(driver, window_handle, uc_lock=True):
16321632
and driver._is_using_uc
16331633
and uc_lock
16341634
):
1635-
gui_lock = fasteners.InterProcessLock(
1636-
constants.MultiBrowser.PYAUTOGUILOCK
1637-
)
1635+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
16381636
with gui_lock:
16391637
driver.switch_to.window(window_handle)
16401638
else:

seleniumbase/undetected/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import subprocess
66
import sys
77
import time
8+
from filelock import FileLock
89
import selenium.webdriver.chrome.service
910
import selenium.webdriver.chrome.webdriver
1011
import selenium.webdriver.common.service
@@ -117,6 +118,7 @@ def __init__(
117118
self.patcher = None
118119
import fasteners
119120
from seleniumbase.fixtures import constants
121+
from seleniumbase.fixtures import shared_utils
120122
if patch_driver:
121123
uc_lock = fasteners.InterProcessLock(
122124
constants.MultiBrowser.DRIVER_FIXING_LOCK
@@ -284,10 +286,11 @@ def __init__(
284286
options.binary_location, *options.arguments
285287
)
286288
else:
287-
gui_lock = fasteners.InterProcessLock(
288-
constants.MultiBrowser.PYAUTOGUILOCK
289-
)
289+
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
290290
with gui_lock:
291+
shared_utils.make_writable(
292+
constants.MultiBrowser.PYAUTOGUILOCK
293+
)
291294
browser = subprocess.Popen(
292295
[options.binary_location, *options.arguments],
293296
stdin=subprocess.PIPE,

0 commit comments

Comments
 (0)