Skip to content

Commit c09c39f

Browse files
committed
Update CDP Mode
1 parent d350947 commit c09c39f

File tree

9 files changed

+105
-91
lines changed

9 files changed

+105
-91
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -443,14 +443,14 @@ def has_captcha(text):
443443
"<title>403 Forbidden</title>" in text
444444
or "Permission Denied</title>" in text
445445
or 'id="challenge-error-text"' in text
446+
or "/challenge-platform/h/b/" in text
446447
or "<title>Just a moment..." in text
447448
or 'action="/?__cf_chl_f_tk' in text
448449
or 'id="challenge-widget-' in text
449450
or 'src="chromedriver.js"' in text
450451
or 'class="g-recaptcha"' in text
451452
or 'content="Pixelscan"' in text
452453
or 'id="challenge-form"' in text
453-
or "/challenge-platform" in text
454454
or "window._cf_chl_opt" in text
455455
or "/recaptcha/api.js" in text
456456
or "/turnstile/" in text
@@ -656,10 +656,8 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
656656
safe_url = False
657657

658658
if (
659-
hasattr(driver, "_is_using_cdp")
660-
and driver._is_using_cdp
661-
and hasattr(driver, "cdp")
662-
and driver.cdp
659+
getattr(driver, "_is_using_cdp", None)
660+
and getattr(driver, "cdp", None)
663661
and hasattr(driver.cdp, "loop")
664662
):
665663
# CDP Mode was already initialized
@@ -1042,7 +1040,7 @@ def uc_click(
10421040
def verify_pyautogui_has_a_headed_browser(driver):
10431041
"""PyAutoGUI requires a headed browser so that it can
10441042
focus on the correct element when performing actions."""
1045-
if hasattr(driver, "_is_hidden") and driver._is_hidden:
1043+
if getattr(driver, "_is_hidden", None):
10461044
raise Exception(
10471045
"PyAutoGUI can't be used in headless mode!"
10481046
)
@@ -1142,8 +1140,7 @@ def get_configured_pyautogui(pyautogui_copy):
11421140
and "DISPLAY" in os.environ.keys()
11431141
):
11441142
if (
1145-
hasattr(sb_config, "_pyautogui_x11_display")
1146-
and sb_config._pyautogui_x11_display
1143+
getattr(sb_config, "_pyautogui_x11_display", None)
11471144
and hasattr(pyautogui_copy._pyautogui_x11, "_display")
11481145
and (
11491146
sb_config._pyautogui_x11_display
@@ -1300,8 +1297,12 @@ def uc_gui_click_x_y(driver, x, y, timeframe=0.25):
13001297
def _on_a_cf_turnstile_page(driver):
13011298
source = driver.get_page_source()
13021299
if (
1303-
'data-callback="onCaptchaSuccess"' in source
1304-
or "/challenge-platform/scripts/" in source
1300+
(
1301+
'data-callback="onCaptchaSuccess"' in source
1302+
and 'title="reCAPTCHA"' not in source
1303+
and 'id="recaptcha-token"' not in source
1304+
)
1305+
or "/challenge-platform/h/b/" in source
13051306
or 'id="challenge-widget-' in source
13061307
or "challenges.cloudf" in source
13071308
or "cf-turnstile-" in source

seleniumbase/core/sb_cdp.py

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,22 +1149,60 @@ def open_new_tab(self, url=None, switch_to=True):
11491149
if not isinstance(url, str):
11501150
url = "about:blank"
11511151
if hasattr(driver, "cdp_base"):
1152-
self.loop.run_until_complete(self.page.get(url, new_tab=True))
1152+
try:
1153+
self.loop.run_until_complete(self.page.get(url, new_tab=True))
1154+
except Exception:
1155+
original_targets = self.loop.run_until_complete(
1156+
self.page.send(mycdp.target.get_targets())
1157+
)
1158+
tab_url = driver.cdp_base.tabs[0].websocket_url
1159+
if not self.driver.is_connected():
1160+
self.driver.connect()
1161+
self.driver.open_new_tab()
1162+
targets = self.loop.run_until_complete(
1163+
self.page.send(mycdp.target.get_targets())
1164+
)
1165+
new_targets = []
1166+
for target in targets:
1167+
if target not in original_targets:
1168+
new_targets.append(target)
1169+
if new_targets:
1170+
found_target = new_targets[0]
1171+
t_str = str(new_targets[0])
1172+
target_id = (
1173+
t_str.split("target_id=TargetID('")[-1].split("')")[0]
1174+
)
1175+
pre_tab_url = tab_url.split("/page/")[0] + "/page/"
1176+
new_tab_url = pre_tab_url + target_id
1177+
new_tab = cdp_tab.Tab(
1178+
new_tab_url, found_target, driver.cdp_base
1179+
)
1180+
driver.cdp_base.targets.append(new_tab)
1181+
driver.cdp_base.tabs.append(new_tab)
1182+
self.driver.disconnect()
1183+
self.switch_to_newest_tab()
1184+
self.open(url)
1185+
return
1186+
elif getattr(sb_config, "guest_mode", None):
1187+
print(" open_new_tab() failed! (Known Guest Mode issue)")
11531188
if switch_to:
11541189
self.switch_to_newest_tab()
11551190
return
11561191

11571192
target_id = self.loop.run_until_complete(
11581193
self.page.send(mycdp.target.create_target(url))
11591194
)
1195+
if not target_id and getattr(sb_config, "guest_mode", None):
1196+
print(" open_new_tab() failed! (Known Guest Mode issue)")
11601197
found_target = None
11611198
targets = self.loop.run_until_complete(
11621199
self.page.send(mycdp.target.get_targets())
11631200
)
1164-
for target in targets:
1165-
if str(target_id) in str(target):
1166-
found_target = target
1167-
break
1201+
if target_id:
1202+
for target in targets:
1203+
if str(target_id) in str(target):
1204+
found_target = target
1205+
break
11681206
if found_target:
11691207
tab_url = driver.tabs[0].websocket_url
11701208
pre_tab_url = tab_url.split("/page/")[0] + "/page/"
@@ -1875,7 +1913,7 @@ def _on_a_cf_turnstile_page(self, source=None):
18751913
and 'title="reCAPTCHA"' not in source
18761914
and 'id="recaptcha-token"' not in source
18771915
)
1878-
or "/challenge-platform/scripts/" in source
1916+
or "/challenge-platform/h/b/" in source
18791917
or 'id="challenge-widget-' in source
18801918
or "challenges.cloudf" in source
18811919
or "cf-turnstile-" in source

seleniumbase/fixtures/base_case.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -228,25 +228,18 @@ def open(self, url):
228228
self.cdp.open(url)
229229
return
230230
elif (
231-
hasattr(self.driver, "_is_using_uc")
232-
and self.driver._is_using_uc
233-
# and hasattr(self.driver, "_is_using_auth")
234-
# and self.driver._is_using_auth
235-
and (
236-
not hasattr(self.driver, "_is_using_cdp")
237-
or not self.driver._is_using_cdp
238-
)
231+
getattr(self.driver, "_is_using_uc", None)
232+
# and getattr(self.driver, "_is_using_auth", None)
233+
and not getattr(self.driver, "_is_using_cdp", None)
239234
):
240235
# Auth in UC Mode requires CDP Mode
241236
# (and now we're always forcing it)
242237
logging.info("open() in UC Mode now always activates CDP Mode.")
243238
self.activate_cdp_mode(url)
244239
return
245240
elif (
246-
hasattr(self.driver, "_is_using_uc")
247-
and self.driver._is_using_uc
248-
and hasattr(self.driver, "_is_using_cdp")
249-
and self.driver._is_using_cdp
241+
getattr(self.driver, "_is_using_uc", None)
242+
and getattr(self.driver, "_is_using_cdp", None)
250243
):
251244
self.disconnect()
252245
self.cdp.open(url)
@@ -3979,10 +3972,8 @@ def open_new_window(self, switch_to=True):
39793972
self.cdp.open_new_tab(url=url, switch_to=switch_to)
39803973
return
39813974
elif (
3982-
hasattr(self.driver, "_is_using_uc")
3983-
and self.driver._is_using_uc
3984-
and hasattr(self.driver, "_is_using_cdp")
3985-
and self.driver._is_using_cdp
3975+
getattr(self.driver, "_is_using_uc", None)
3976+
and getattr(self.driver, "_is_using_cdp", None)
39863977
):
39873978
self.disconnect()
39883979
self.cdp.open_new_tab(url=url, switch_to=switch_to)
@@ -5037,7 +5028,7 @@ def deactivate_design_mode(self, url=None):
50375028

50385029
def activate_cdp_mode(self, url=None, **kwargs):
50395030
"""Activate CDP Mode with the URL and kwargs."""
5040-
if hasattr(self.driver, "_is_using_uc") and self.driver._is_using_uc:
5031+
if getattr(self.driver, "_is_using_uc", None):
50415032
if self.__is_cdp_swap_needed():
50425033
return # CDP Mode is already active
50435034
if not self.is_connected():

seleniumbase/fixtures/page_actions.py

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,11 +1646,7 @@ def switch_to_frame(
16461646

16471647

16481648
def __switch_to_window(driver, window_handle, uc_lock=True):
1649-
if (
1650-
hasattr(driver, "_is_using_uc")
1651-
and driver._is_using_uc
1652-
and uc_lock
1653-
):
1649+
if getattr(driver, "_is_using_uc", None) and uc_lock:
16541650
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
16551651
with gui_lock:
16561652
driver.switch_to.window(window_handle)
@@ -1734,8 +1730,7 @@ def switch_to_window(
17341730

17351731
def _reconnect_if_disconnected(driver):
17361732
if (
1737-
hasattr(driver, "_is_using_uc")
1738-
and driver._is_using_uc
1733+
getattr(driver, "_is_using_uc", None)
17391734
and hasattr(driver, "is_connected")
17401735
and not driver.is_connected()
17411736
):
@@ -1757,24 +1752,17 @@ def open_url(driver, url):
17571752
driver.cdp.open(url)
17581753
return
17591754
elif (
1760-
hasattr(driver, "_is_using_uc")
1761-
and driver._is_using_uc
1762-
# and hasattr(driver, "_is_using_auth")
1763-
# and driver._is_using_auth
1764-
and (
1765-
not hasattr(driver, "_is_using_cdp")
1766-
or not driver._is_using_cdp
1767-
)
1755+
getattr(driver, "_is_using_uc", None)
1756+
# and getattr(driver, "_is_using_auth", None)
1757+
and not getattr(driver, "_is_using_cdp", None)
17681758
):
17691759
# Auth in UC Mode requires CDP Mode
17701760
# (and now we're always forcing it)
17711761
driver.uc_activate_cdp_mode(url)
17721762
return
17731763
elif (
1774-
hasattr(driver, "_is_using_uc")
1775-
and driver._is_using_uc
1776-
and hasattr(driver, "_is_using_cdp")
1777-
and driver._is_using_cdp
1764+
getattr(driver, "_is_using_uc", None)
1765+
and getattr(driver, "_is_using_cdp", None)
17781766
):
17791767
driver.disconnect()
17801768
driver.cdp.open(url)

seleniumbase/fixtures/shared_utils.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ def fix_url_as_needed(url):
147147

148148
def reconnect_if_disconnected(driver):
149149
if (
150-
hasattr(driver, "_is_using_uc")
151-
and driver._is_using_uc
150+
getattr(driver, "_is_using_uc", None)
152151
and hasattr(driver, "is_connected")
153152
and not driver.is_connected()
154153
):

seleniumbase/undetected/__init__.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def __init__(
163163
from seleniumbase import config as sb_config
164164
if (
165165
(("-n" in sys.argv) or (" -n=" in arg_join) or ("-c" in sys.argv))
166-
or (hasattr(sb_config, "multi_proxy") and sb_config.multi_proxy)
166+
or getattr(sb_config, "multi_proxy", None)
167167
or not special_port_free
168168
):
169169
debug_port = selenium.webdriver.common.service.utils.free_port()
@@ -197,9 +197,7 @@ def __init__(
197197
except IndexError:
198198
pass
199199
if not user_data_dir:
200-
if hasattr(options, "user_data_dir") and getattr(
201-
options, "user_data_dir", None
202-
):
200+
if getattr(options, "user_data_dir", None):
203201
options.add_argument(
204202
"--user-data-dir=%s" % options.user_data_dir
205203
)
@@ -405,9 +403,7 @@ def get(self, url):
405403

406404
def add_cdp_listener(self, event_name, callback):
407405
if (
408-
hasattr(self, "reactor")
409-
and self.reactor
410-
and self.reactor is not None
406+
getattr(self, "reactor", None)
411407
and isinstance(self.reactor, Reactor)
412408
):
413409
self.reactor.add_event_handler(event_name, callback)
@@ -416,8 +412,7 @@ def add_cdp_listener(self, event_name, callback):
416412

417413
def clear_cdp_listeners(self):
418414
if (
419-
hasattr(self, "reactor")
420-
and self.reactor
415+
getattr(self, "reactor", None)
421416
and isinstance(self.reactor, Reactor)
422417
):
423418
self.reactor.handlers.clear()
@@ -526,7 +521,13 @@ def connect(self):
526521
with suppress(Exception):
527522
for window_handle in self.window_handles:
528523
self.switch_to.window(window_handle)
529-
if self.current_url.startswith("chrome-extension://"):
524+
current_url = None
525+
if hasattr(self, "cdp") and hasattr(self.cdp, "driver"):
526+
with suppress(Exception):
527+
current_url = self.cdp.get_current_url()
528+
if not current_url:
529+
current_url = self.current_url
530+
if current_url.startswith("chrome-extension://"):
530531
# https://issues.chromium.org/issues/396611138
531532
# (Remove the Linux conditional when resolved)
532533
# (So that close() is always called)

0 commit comments

Comments
 (0)