Skip to content

Commit b9aa24d

Browse files
committed
Update CDP Mode
1 parent 8e9757e commit b9aa24d

File tree

1 file changed

+51
-21
lines changed

1 file changed

+51
-21
lines changed

seleniumbase/core/sb_cdp.py

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,17 @@ def get_gui_element_rect(self, selector, timeout=None):
13331333
x = window_rect["x"] + element_rect["x"]
13341334
y = w_bottom_y - viewport_height + element_rect["y"]
13351335
y_scroll_offset = window_rect["pageYOffset"]
1336+
if (
1337+
hasattr(sb_config, "_cdp_browser")
1338+
and sb_config._cdp_browser == "opera"
1339+
):
1340+
# Handle special case where Opera side panel shifts coordinates
1341+
x_offset = window_rect["outerWidth"] - window_rect["innerWidth"]
1342+
if x_offset > 56:
1343+
x_offset = 56
1344+
elif x_offset < 22:
1345+
x_offset = 0
1346+
x = x + x_offset
13361347
y = y - y_scroll_offset
13371348
x = x + window_rect["scrollX"]
13381349
y = y + window_rect["scrollY"]
@@ -1674,11 +1685,6 @@ def __gui_click_x_y(self, x, y, timeframe=0.25, uc_lock=False):
16741685
import pyautogui
16751686
pyautogui = self.__get_configured_pyautogui(pyautogui)
16761687
screen_width, screen_height = pyautogui.size()
1677-
if (
1678-
hasattr(sb_config, "_cdp_browser")
1679-
and sb_config._cdp_browser == "opera"
1680-
):
1681-
x = x + 55
16821688
if x < 0 or y < 0 or x > screen_width or y > screen_height:
16831689
raise Exception(
16841690
"PyAutoGUI cannot click on point (%s, %s)"
@@ -1731,8 +1737,8 @@ def gui_click_x_y(self, x, y, timeframe=0.25):
17311737
self.__add_light_pause()
17321738
self.__set_window_rect(win_x, win_y, width, height)
17331739
self.__add_light_pause()
1734-
x = x * width_ratio
1735-
y = y * width_ratio
1740+
x = x * (width_ratio + 0.03)
1741+
y = y * (width_ratio - 0.03)
17361742
self.bring_active_window_to_front()
17371743
self.__gui_click_x_y(x, y, timeframe=timeframe, uc_lock=False)
17381744

@@ -1760,8 +1766,43 @@ def _on_a_cf_turnstile_page(self):
17601766
return True
17611767
return False
17621768

1769+
def _on_a_g_recaptcha_page(self):
1770+
time.sleep(0.042)
1771+
source = self.get_page_source()
1772+
if not source or len(source) < 400:
1773+
time.sleep(0.22)
1774+
source = self.get_page_source()
1775+
if (
1776+
'id="recaptcha-token"' in source
1777+
or 'title="reCAPTCHA"' in source
1778+
):
1779+
return True
1780+
return False
1781+
1782+
def __gui_click_recaptcha(self):
1783+
selector = None
1784+
if self.is_element_visible('iframe[title="reCAPTCHA"]'):
1785+
selector = 'iframe[title="reCAPTCHA"]'
1786+
else:
1787+
return
1788+
with suppress(Exception):
1789+
time.sleep(0.08)
1790+
element_rect = self.get_gui_element_rect(selector, timeout=1)
1791+
e_x = element_rect["x"]
1792+
e_y = element_rect["y"]
1793+
x = e_x + 29
1794+
y = e_y + 35
1795+
sb_config._saved_cf_x_y = (x, y)
1796+
time.sleep(0.08)
1797+
self.gui_click_x_y(x, y)
1798+
17631799
def gui_click_captcha(self):
1764-
if not self._on_a_cf_turnstile_page():
1800+
if self._on_a_cf_turnstile_page():
1801+
pass
1802+
elif self._on_a_g_recaptcha_page():
1803+
self.__gui_click_recaptcha()
1804+
return
1805+
else:
17651806
return
17661807
selector = None
17671808
if (
@@ -1926,7 +1967,7 @@ def gui_click_captcha(self):
19261967
if not shared_utils.is_windows():
19271968
y = e_y + 32
19281969
else:
1929-
y = e_y + 22
1970+
y = e_y + 28
19301971
sb_config._saved_cf_x_y = (x, y)
19311972
time.sleep(0.08)
19321973
self.gui_click_x_y(x, y)
@@ -1936,12 +1977,6 @@ def __gui_drag_drop(self, x1, y1, x2, y2, timeframe=0.25, uc_lock=False):
19361977
import pyautogui
19371978
pyautogui = self.__get_configured_pyautogui(pyautogui)
19381979
screen_width, screen_height = pyautogui.size()
1939-
if (
1940-
hasattr(sb_config, "_cdp_browser")
1941-
and sb_config._cdp_browser == "opera"
1942-
):
1943-
x1 = x1 + 55
1944-
x2 = x2 + 55
19451980
if x1 < 0 or y1 < 0 or x1 > screen_width or y1 > screen_height:
19461981
raise Exception(
19471982
"PyAutoGUI cannot drag-drop from point (%s, %s)"
@@ -2033,11 +2068,6 @@ def __gui_hover_x_y(self, x, y, timeframe=0.25, uc_lock=False):
20332068
import pyautogui
20342069
pyautogui = self.__get_configured_pyautogui(pyautogui)
20352070
screen_width, screen_height = pyautogui.size()
2036-
if (
2037-
hasattr(sb_config, "_cdp_browser")
2038-
and sb_config._cdp_browser == "opera"
2039-
):
2040-
x = x + 55
20412071
if x < 0 or y < 0 or x > screen_width or y > screen_height:
20422072
raise Exception(
20432073
"PyAutoGUI cannot hover on point (%s, %s)"
@@ -2118,7 +2148,7 @@ def gui_hover_element(self, selector, timeframe=0.25):
21182148
if width > 0 and height > 0:
21192149
x, y = self.get_gui_element_center(selector)
21202150
self.bring_active_window_to_front()
2121-
self.__gui_hover_x_y(x, y, timeframe=timeframe)
2151+
self.__gui_hover_x_y(x, y, timeframe=timeframe, uc_lock=False)
21222152
self.__slow_mode_pause_if_set()
21232153
self.loop.run_until_complete(self.page.wait())
21242154

0 commit comments

Comments
 (0)