66import sys
77import time
88from contextlib import suppress
9+ from filelock import FileLock
910from seleniumbase import config as sb_config
1011from seleniumbase .config import settings
1112from 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 )
0 commit comments