Skip to content

Commit 884b428

Browse files
committed
Update CDP Mode
1 parent c63bd7a commit 884b428

File tree

8 files changed

+57
-15
lines changed

8 files changed

+57
-15
lines changed

seleniumbase/core/browser_launcher.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,11 @@ def has_captcha(text):
445445
or 'action="/?__cf_chl_f_tk' in text
446446
or 'id="challenge-widget-' in text
447447
or 'src="chromedriver.js"' in text
448+
or "com/recaptcha/api.js" in text
448449
or 'class="g-recaptcha"' in text
449450
or 'content="Pixelscan"' in text
450451
or 'id="challenge-form"' in text
451452
or "window._cf_chl_opt" in text
452-
or "/recaptcha/api.js" in text
453453
or "/turnstile/" in text
454454
):
455455
return True
@@ -694,6 +694,7 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
694694
xvfb=xvfb,
695695
xvfb_metrics=xvfb_metrics,
696696
browser_executable_path=binary_location,
697+
mobile=getattr(sb_config, "_cdp_mobile_mode", None),
697698
)
698699
)
699700
loop.run_until_complete(driver.cdp_base.wait(0))
@@ -2761,6 +2762,8 @@ def _set_chrome_options(
27612762
included_disabled_features.append("SidePanelPinning")
27622763
included_disabled_features.append("UserAgentClientHint")
27632764
included_disabled_features.append("DisableLoadExtensionCommandLineSwitch")
2765+
included_disabled_features.append("WebAuthentication")
2766+
included_disabled_features.append("PasskeyAuth")
27642767
for item in extra_disabled_features:
27652768
if item not in included_disabled_features:
27662769
included_disabled_features.append(item)
@@ -3056,11 +3059,6 @@ def get_driver(
30563059
if _special_binary_exists(binary_location, "atlas"):
30573060
driver_dir = DRIVER_DIR_ATLAS
30583061
sb_config._cdp_browser = "atlas"
3059-
if undetectable and mobile_emulator:
3060-
# For stealthy mobile mode, see the CDP Mode examples
3061-
# to learn how to properly configure it.
3062-
user_agent = None # Undo the override
3063-
mobile_emulator = False # Instead, set from CDP Mode
30643062
if (
30653063
hasattr(sb_config, "settings")
30663064
and getattr(sb_config.settings, "NEW_DRIVER_DIR", None)
@@ -3075,6 +3073,23 @@ def get_driver(
30753073
if browser_name in constants.ChromiumSubs.chromium_subs:
30763074
browser_name = "chrome"
30773075
browser_name = browser_name.lower()
3076+
if is_using_uc(undetectable, browser_name):
3077+
if ad_block_on:
3078+
sb_config.ad_block_on = True
3079+
else:
3080+
sb_config.ad_block_on = False
3081+
if disable_csp:
3082+
sb_config.disable_csp = True
3083+
else:
3084+
sb_config.disable_csp = False
3085+
if mobile_emulator:
3086+
# For stealthy mobile mode, see the CDP Mode examples
3087+
# to learn how to properly configure it.
3088+
user_agent = None # Undo the override
3089+
mobile_emulator = False # Instead, set from CDP Mode
3090+
sb_config._cdp_mobile_mode = True
3091+
else:
3092+
sb_config._cdp_mobile_mode = False
30783093
if headless2 and browser_name == constants.Browser.FIREFOX:
30793094
headless2 = False # Only for Chromium
30803095
headless = True

seleniumbase/core/sb_cdp.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,8 +1922,8 @@ def _on_a_cf_turnstile_page(self, source=None):
19221922
return False
19231923

19241924
def _on_a_g_recaptcha_page(self, source=None):
1925-
time.sleep(0.25)
1926-
self.loop.run_until_complete(self.page.wait(0.25))
1925+
time.sleep(0.2)
1926+
self.loop.run_until_complete(self.page.wait(0.2))
19271927
source = self.get_page_source()
19281928
if (
19291929
(
@@ -1934,7 +1934,7 @@ def _on_a_g_recaptcha_page(self, source=None):
19341934
):
19351935
self.loop.run_until_complete(self.page.wait(0.1))
19361936
return True
1937-
elif "/recaptcha/api.js" in source:
1937+
elif "com/recaptcha/api.js" in source:
19381938
time.sleep(1.6) # Still loading
19391939
self.loop.run_until_complete(self.page.wait(0.1))
19401940
return True
@@ -2330,6 +2330,9 @@ def gui_hover_element(self, selector, timeframe=0.25):
23302330
self.loop.run_until_complete(self.page.wait())
23312331

23322332
def gui_hover_and_click(self, hover_selector, click_selector):
2333+
if getattr(sb_config, "_cdp_mobile_mode", None):
2334+
self.select(click_selector).click()
2335+
return
23332336
gui_lock = FileLock(constants.MultiBrowser.PYAUTOGUILOCK)
23342337
with gui_lock:
23352338
self.__make_sure_pyautogui_lock_is_writable()

seleniumbase/fixtures/base_case.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15585,7 +15585,7 @@ def setUp(self, masterqa_mode=False):
1558515585
"Invalid input for Mobile Emulator device metrics!\n"
1558615586
"Expecting a comma-separated string with integer values\n"
1558715587
"for Width/Height, and an int or float for Pixel-Ratio.\n"
15588-
'Example: --metrics="411,731,3" '
15588+
'Example: --metrics="412,732,3" '
1558915589
)
1559015590
if len(metrics_list) != 3:
1559115591
raise Exception(exception_string)

seleniumbase/plugins/pytest_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def pytest_addoption(parser):
625625
help="""Designates the three device metrics of the mobile
626626
emulator: CSS Width, CSS Height, and Pixel-Ratio.
627627
Format: A comma-separated string with the 3 values.
628-
Examples: "375,734,5" or "411,731,3" or "390,715,3"
628+
Examples: "375,734,5" or "412,732,3" or "390,715,3"
629629
Default: None. (Will use default values if None)""",
630630
)
631631
parser.addoption(

seleniumbase/plugins/selenium_plugin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ def options(self, parser, env):
367367
help="""Designates the three device metrics of the mobile
368368
emulator: CSS Width, CSS Height, and Pixel-Ratio.
369369
Format: A comma-separated string with the 3 values.
370-
Examples: "375,734,5" or "411,731,3" or "390,715,3"
370+
Examples: "375,734,5" or "412,732,3" or "390,715,3"
371371
Default: None. (Will use default values if None)""",
372372
)
373373
parser.addoption(

seleniumbase/undetected/cdp_driver/browser.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ async def get(
327327
_cdp_platform = None
328328
_cdp_disable_csp = None
329329
_cdp_geolocation = None
330+
_cdp_mobile_mode = None
330331
_cdp_recorder = None
331332
_cdp_ad_block = None
332333
if getattr(sb_config, "_cdp_timezone", None):
@@ -339,8 +340,12 @@ async def get(
339340
_cdp_platform = sb_config._cdp_platform
340341
if getattr(sb_config, "_cdp_geolocation", None):
341342
_cdp_geolocation = sb_config._cdp_geolocation
343+
if getattr(sb_config, "_cdp_mobile_mode", None):
344+
_cdp_mobile_mode = sb_config._cdp_mobile_mode
342345
if getattr(sb_config, "ad_block_on", None):
343346
_cdp_ad_block = sb_config.ad_block_on
347+
if getattr(sb_config, "disable_csp", None):
348+
_cdp_disable_csp = sb_config.disable_csp
344349
if "timezone" in kwargs:
345350
_cdp_timezone = kwargs["timezone"]
346351
elif "tzone" in kwargs:
@@ -361,12 +366,14 @@ async def get(
361366
_cdp_platform = kwargs["plat"]
362367
if "disable_csp" in kwargs:
363368
_cdp_disable_csp = kwargs["disable_csp"]
364-
elif hasattr(sb_config, "disable_csp"):
365-
_cdp_disable_csp = sb_config.disable_csp
366369
if "geolocation" in kwargs:
367370
_cdp_geolocation = kwargs["geolocation"]
368371
elif "geoloc" in kwargs:
369372
_cdp_geolocation = kwargs["geoloc"]
373+
if "ad_block" in kwargs:
374+
_cdp_ad_block = kwargs["ad_block"]
375+
if "mobile" in kwargs:
376+
_cdp_mobile_mode = kwargs["mobile"]
370377
if "recorder" in kwargs:
371378
_cdp_recorder = kwargs["recorder"]
372379
await connection.sleep(0.01)
@@ -422,6 +429,12 @@ async def get(
422429
await connection.set_geolocation(_cdp_geolocation)
423430
if _cdp_disable_csp:
424431
await connection.send(cdp.page.set_bypass_csp(enabled=True))
432+
if _cdp_mobile_mode:
433+
await connection.send(
434+
cdp.emulation.set_device_metrics_override(
435+
width=412, height=732, device_scale_factor=3, mobile=True
436+
)
437+
)
425438
# (The code below is for the Chrome 142 extension fix)
426439
if (
427440
getattr(sb_config, "_cdp_proxy", None)

seleniumbase/undetected/cdp_driver/cdp_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ async def start(
281281
proxy: Optional[str] = None, # "host:port" or "user:pass@host:port"
282282
tzone: Optional[str] = None, # Eg "America/New_York", "Asia/Kolkata"
283283
geoloc: Optional[list | tuple] = None, # Eg (48.87645, 2.26340)
284+
mobile: Optional[bool] = None, # Use Mobile Mode with default args
284285
disable_csp: Optional[str] = None, # Disable content security policy
285286
extension_dir: Optional[str] = None, # Chrome extension directory
286287
**kwargs: Optional[dict],
@@ -353,6 +354,15 @@ async def start(
353354
guest = True
354355
else:
355356
guest = False
357+
if mobile is None:
358+
if "--mobile" in sys_argv:
359+
mobile = True
360+
else:
361+
mobile = False
362+
if mobile:
363+
sb_config._cdp_mobile_mode = True
364+
else:
365+
sb_config._cdp_mobile_mode = False
356366
if ad_block is None:
357367
if "--ad-block" in sys_argv or "--ad_block" in sys_argv:
358368
ad_block = True

seleniumbase/undetected/cdp_driver/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ def __call__(self):
206206
"OptimizationTargetPrediction,OptimizationGuideModelDownloading,"
207207
"SidePanelPinning,UserAgentClientHint,PrivacySandboxSettings4,"
208208
"OptimizationHintsFetching,InterestFeedContentSuggestions,"
209-
"DisableLoadExtensionCommandLineSwitch"
209+
"DisableLoadExtensionCommandLineSwitch,"
210+
"WebAuthentication,PasskeyAuth"
210211
]
211212
if self.expert:
212213
args += [

0 commit comments

Comments
 (0)