6
6
import sys
7
7
import time
8
8
from contextlib import suppress
9
+ from filelock import FileLock
9
10
from seleniumbase import config as sb_config
10
11
from seleniumbase .config import settings
11
12
from seleniumbase .fixtures import constants
@@ -1065,24 +1066,42 @@ def medimize(self):
1065
1066
time .sleep (0.044 )
1066
1067
return self .loop .run_until_complete (self .page .medimize ())
1067
1068
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 (
1071
1092
self .page .set_window_size (
1072
1093
left = x , top = y , width = width , height = height )
1073
1094
)
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 )
1079
1098
1080
1099
def reset_window_size (self ):
1081
1100
x = settings .WINDOW_START_X
1082
1101
y = settings .WINDOW_START_Y
1083
1102
width = settings .CHROME_START_WIDTH
1084
1103
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 )
1086
1105
self .__add_light_pause ()
1087
1106
1088
1107
def open_new_window (self , url = None , switch_to = True ):
@@ -1548,9 +1567,7 @@ def gui_press_key(self, key):
1548
1567
self .__install_pyautogui_if_missing ()
1549
1568
import pyautogui
1550
1569
pyautogui = self .__get_configured_pyautogui (pyautogui )
1551
- gui_lock = fasteners .InterProcessLock (
1552
- constants .MultiBrowser .PYAUTOGUILOCK
1553
- )
1570
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1554
1571
with gui_lock :
1555
1572
self .__make_sure_pyautogui_lock_is_writable ()
1556
1573
pyautogui .press (key )
@@ -1562,9 +1579,7 @@ def gui_press_keys(self, keys):
1562
1579
self .__install_pyautogui_if_missing ()
1563
1580
import pyautogui
1564
1581
pyautogui = self .__get_configured_pyautogui (pyautogui )
1565
- gui_lock = fasteners .InterProcessLock (
1566
- constants .MultiBrowser .PYAUTOGUILOCK
1567
- )
1582
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1568
1583
with gui_lock :
1569
1584
self .__make_sure_pyautogui_lock_is_writable ()
1570
1585
for key in keys :
@@ -1577,9 +1592,7 @@ def gui_write(self, text):
1577
1592
self .__install_pyautogui_if_missing ()
1578
1593
import pyautogui
1579
1594
pyautogui = self .__get_configured_pyautogui (pyautogui )
1580
- gui_lock = fasteners .InterProcessLock (
1581
- constants .MultiBrowser .PYAUTOGUILOCK
1582
- )
1595
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1583
1596
with gui_lock :
1584
1597
self .__make_sure_pyautogui_lock_is_writable ()
1585
1598
pyautogui .write (text )
@@ -1598,9 +1611,7 @@ def __gui_click_x_y(self, x, y, timeframe=0.25, uc_lock=False):
1598
1611
% (x , y , screen_width , screen_height )
1599
1612
)
1600
1613
if uc_lock :
1601
- gui_lock = fasteners .InterProcessLock (
1602
- constants .MultiBrowser .PYAUTOGUILOCK
1603
- )
1614
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1604
1615
with gui_lock : # Prevent issues with multiple processes
1605
1616
self .__make_sure_pyautogui_lock_is_writable ()
1606
1617
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):
1619
1630
pyautogui .click (x = x , y = y )
1620
1631
1621
1632
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 )
1625
1634
with gui_lock : # Prevent issues with multiple processes
1626
1635
self .__make_sure_pyautogui_lock_is_writable ()
1627
1636
self .__install_pyautogui_if_missing ()
@@ -1645,7 +1654,7 @@ def gui_click_x_y(self, x, y, timeframe=0.25):
1645
1654
sb_config ._saved_width_ratio = width_ratio
1646
1655
self .minimize ()
1647
1656
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 )
1649
1658
self .__add_light_pause ()
1650
1659
x = x * width_ratio
1651
1660
y = y * width_ratio
@@ -1831,9 +1840,7 @@ def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
1831
1840
% (x2 , y2 , screen_width , screen_height )
1832
1841
)
1833
1842
if uc_lock :
1834
- gui_lock = fasteners .InterProcessLock (
1835
- constants .MultiBrowser .PYAUTOGUILOCK
1836
- )
1843
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1837
1844
with gui_lock : # Prevent issues with multiple processes
1838
1845
pyautogui .moveTo (x1 , y1 , 0.25 , pyautogui .easeOutQuad )
1839
1846
self .__add_light_pause ()
@@ -1851,9 +1858,7 @@ def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
1851
1858
def gui_drag_drop_points (self , x1 , y1 , x2 , y2 , timeframe = 0.35 ):
1852
1859
"""Use PyAutoGUI to drag-and-drop from one point to another.
1853
1860
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 )
1857
1862
with gui_lock : # Prevent issues with multiple processes
1858
1863
self .__install_pyautogui_if_missing ()
1859
1864
import pyautogui
@@ -1876,7 +1881,7 @@ def gui_drag_drop_points(self, x1, y1, x2, y2, timeframe=0.35):
1876
1881
sb_config ._saved_width_ratio = width_ratio
1877
1882
self .minimize ()
1878
1883
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 )
1880
1885
self .__add_light_pause ()
1881
1886
x1 = x1 * width_ratio
1882
1887
y1 = y1 * (width_ratio - 0.02 )
@@ -1920,9 +1925,7 @@ def __gui_hover_x_y(self, x, y, timeframe=0.25, uc_lock=False):
1920
1925
% (x , y , screen_width , screen_height )
1921
1926
)
1922
1927
if uc_lock :
1923
- gui_lock = fasteners .InterProcessLock (
1924
- constants .MultiBrowser .PYAUTOGUILOCK
1925
- )
1928
+ gui_lock = FileLock (constants .MultiBrowser .PYAUTOGUILOCK )
1926
1929
with gui_lock : # Prevent issues with multiple processes
1927
1930
pyautogui .moveTo (x , y , timeframe , pyautogui .easeOutQuad )
1928
1931
time .sleep (0.056 )
@@ -1936,9 +1939,7 @@ def __gui_hover_x_y(self, x, y, timeframe=0.25, uc_lock=False):
1936
1939
print (" <DEBUG> pyautogui.moveTo(%s, %s)" % (x , y ))
1937
1940
1938
1941
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 )
1942
1943
with gui_lock : # Prevent issues with multiple processes
1943
1944
self .__install_pyautogui_if_missing ()
1944
1945
import pyautogui
@@ -1971,7 +1972,7 @@ def gui_hover_x_y(self, x, y, timeframe=0.25):
1971
1972
if width_ratio < 0.45 or width_ratio > 2.55 :
1972
1973
width_ratio = 1.01
1973
1974
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 )
1975
1976
self .__add_light_pause ()
1976
1977
self .bring_active_window_to_front ()
1977
1978
elif (
@@ -2002,9 +2003,7 @@ def gui_hover_element(self, selector, timeframe=0.25):
2002
2003
self .loop .run_until_complete (self .page .wait ())
2003
2004
2004
2005
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 )
2008
2007
with gui_lock :
2009
2008
self .__make_sure_pyautogui_lock_is_writable ()
2010
2009
self .bring_active_window_to_front ()
@@ -2586,7 +2585,7 @@ class Chrome(CDPMethods):
2586
2585
def __init__ (self , url = None , ** kwargs ):
2587
2586
if not url :
2588
2587
url = "about:blank"
2589
- loop = asyncio .new_event_loop ()
2590
2588
driver = cdp_util .start_sync (** kwargs )
2589
+ loop = asyncio .new_event_loop ()
2591
2590
page = loop .run_until_complete (driver .get (url ))
2592
2591
super ().__init__ (loop , page , driver )
0 commit comments