Skip to content

Commit 0a4808f

Browse files
committed
Add option to capture CDP events in UC Mode
1 parent 0862fbc commit 0a4808f

File tree

9 files changed

+106
-3
lines changed

9 files changed

+106
-3
lines changed

examples/raw_parameter_script.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
sb.enable_sync = False
6767
sb.use_auto_ext = False
6868
sb.undetectable = False
69+
sb.uc_cdp_events = False
6970
sb.uc_subprocess = False
7071
sb.no_sandbox = False
7172
sb.disable_js = False

help_docs/customizing_test_runs.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ pytest my_first_test.py --settings-file=custom_settings.py
164164
--enable-ws # (Enable Web Security on Chromium-based browsers.)
165165
--enable-sync # (Enable "Chrome Sync" on websites.)
166166
--uc | --undetected # (Use undetected-chromedriver to evade bot-detection.)
167+
--uc-cdp-events # (Capture CDP events when running in "--undetected" mode.)
167168
--remote-debug # (Sync to Chrome Remote Debugger chrome://inspect/#devices)
168169
--final-debug # (Enter Debug Mode after each test ends. Don't use with CI!)
169170
--dashboard # (Enable the SeleniumBase Dashboard. Saved at: dashboard.html)

seleniumbase/behave/behave_sb.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
-D enable-ws (Enable Web Security on Chromium-based browsers.)
7373
-D enable-sync (Enable "Chrome Sync".)
7474
-D uc | -D undetected (Use undetected-chromedriver to evade bot-detection)
75+
-D uc-cdp-events (Capture CDP events when running in "--undetected" mode.)
7576
-D remote-debug (Sync to Chrome Remote Debugger chrome://inspect/#devices)
7677
-D dashboard (Enable the SeleniumBase Dashboard. Saved at: dashboard.html)
7778
-D dash-title=STRING (Set the title shown for the generated dashboard.)
@@ -171,6 +172,7 @@ def get_configured_sb(context):
171172
sb.enable_sync = False
172173
sb.use_auto_ext = False
173174
sb.undetectable = False
175+
sb.uc_cdp_events = False
174176
sb.uc_subprocess = False
175177
sb.no_sandbox = False
176178
sb.disable_gpu = False
@@ -509,6 +511,11 @@ def get_configured_sb(context):
509511
if low_key in ["undetected", "undetectable", "uc"]:
510512
sb.undetectable = True
511513
continue
514+
# Handle: -D uc-cdp-events / uc_cdp_events / uc-cdp
515+
if low_key in ["uc-cdp-events", "uc_cdp_events", "uc-cdp"]:
516+
sb.uc_cdp_events = True
517+
sb.undetectable = True
518+
continue
512519
# Handle: -D uc-subprocess / uc_subprocess / uc-sub
513520
if low_key in ["uc-subprocess", "uc_subprocess", "uc-sub"]:
514521
sb.uc_subprocess = True

seleniumbase/core/browser_launcher.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ def _set_chrome_options(
385385
enable_sync,
386386
use_auto_ext,
387387
undetectable,
388+
uc_cdp_events,
388389
uc_subprocess,
389390
no_sandbox,
390391
disable_gpu,
@@ -943,6 +944,7 @@ def get_driver(
943944
enable_sync=False,
944945
use_auto_ext=False,
945946
undetectable=False,
947+
uc_cdp_events=False,
946948
uc_subprocess=False,
947949
no_sandbox=False,
948950
disable_gpu=False,
@@ -981,7 +983,7 @@ def get_driver(
981983
if headless2 and browser_name == constants.Browser.FIREFOX:
982984
headless2 = False # Only for Chromium
983985
headless = True
984-
if uc_subprocess and not undetectable:
986+
if (uc_cdp_events or uc_subprocess) and not undetectable:
985987
undetectable = True
986988
if is_using_uc(undetectable, browser_name) and mobile_emulator:
987989
mobile_emulator = False
@@ -1114,6 +1116,7 @@ def get_driver(
11141116
enable_sync,
11151117
use_auto_ext,
11161118
undetectable,
1119+
uc_cdp_events,
11171120
uc_subprocess,
11181121
no_sandbox,
11191122
disable_gpu,
@@ -1162,6 +1165,7 @@ def get_driver(
11621165
enable_sync,
11631166
use_auto_ext,
11641167
undetectable,
1168+
uc_cdp_events,
11651169
uc_subprocess,
11661170
no_sandbox,
11671171
disable_gpu,
@@ -1214,6 +1218,7 @@ def get_remote_driver(
12141218
enable_sync,
12151219
use_auto_ext,
12161220
undetectable,
1221+
uc_cdp_events,
12171222
uc_subprocess,
12181223
no_sandbox,
12191224
disable_gpu,
@@ -1331,6 +1336,7 @@ def get_remote_driver(
13311336
enable_sync,
13321337
use_auto_ext,
13331338
undetectable,
1339+
uc_cdp_events,
13341340
uc_subprocess,
13351341
no_sandbox,
13361342
disable_gpu,
@@ -1562,6 +1568,7 @@ def get_remote_driver(
15621568
enable_sync,
15631569
use_auto_ext,
15641570
undetectable,
1571+
uc_cdp_events,
15651572
uc_subprocess,
15661573
no_sandbox,
15671574
disable_gpu,
@@ -1683,6 +1690,7 @@ def get_local_driver(
16831690
enable_sync,
16841691
use_auto_ext,
16851692
undetectable,
1693+
uc_cdp_events,
16861694
uc_subprocess,
16871695
no_sandbox,
16881696
disable_gpu,
@@ -2373,6 +2381,7 @@ def get_local_driver(
23732381
enable_sync,
23742382
use_auto_ext,
23752383
undetectable,
2384+
uc_cdp_events,
23762385
uc_subprocess,
23772386
no_sandbox,
23782387
disable_gpu,
@@ -2425,6 +2434,7 @@ def get_local_driver(
24252434
enable_sync,
24262435
use_auto_ext,
24272436
undetectable,
2437+
uc_cdp_events,
24282438
uc_subprocess,
24292439
no_sandbox,
24302440
disable_gpu,
@@ -2707,6 +2717,7 @@ def get_local_driver(
27072717
constants.MultiBrowser.DRIVER_FIXING_LOCK
27082718
)
27092719
with uc_lock: # Avoid multithreaded issues
2720+
cdp_events = uc_cdp_events
27102721
try:
27112722
uc_path = None
27122723
if os.path.exists(LOCAL_UC_DRIVER):
@@ -2716,6 +2727,7 @@ def get_local_driver(
27162727
options=chrome_options,
27172728
user_data_dir=user_data_dir,
27182729
driver_executable_path=uc_path,
2730+
enable_cdp_events=cdp_events,
27192731
headless=False, # Xvfb needed!
27202732
version_main=uc_chrome_version,
27212733
use_subprocess=True, # Always!
@@ -2734,6 +2746,7 @@ def get_local_driver(
27342746
options=chrome_options,
27352747
user_data_dir=user_data_dir,
27362748
driver_executable_path=uc_path,
2749+
enable_cdp_events=cdp_events,
27372750
headless=False, # Xvfb needed!
27382751
version_main=uc_chrome_version,
27392752
use_subprocess=True, # Always!
@@ -2828,6 +2841,7 @@ def get_local_driver(
28282841
enable_sync,
28292842
use_auto_ext,
28302843
undetectable,
2844+
uc_cdp_events,
28312845
uc_subprocess,
28322846
no_sandbox,
28332847
disable_gpu,

seleniumbase/fixtures/base_case.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,6 +2603,7 @@ def __element_click(self, element):
26032603
self.__check_scope()
26042604
if (
26052605
not self.undetectable
2606+
or self.uc_cdp_events
26062607
or self.__uc_frame_layer > 0
26072608
or not hasattr(element, "uc_click")
26082609
):
@@ -3374,6 +3375,7 @@ def get_new_driver(
33743375
enable_sync=None,
33753376
use_auto_ext=None,
33763377
undetectable=None,
3378+
uc_cdp_events=None,
33773379
uc_subprocess=None,
33783380
no_sandbox=None,
33793381
disable_gpu=None,
@@ -3425,6 +3427,7 @@ def get_new_driver(
34253427
enable_sync - the option to enable the Chrome Sync feature (Chrome)
34263428
use_auto_ext - the option to enable Chrome's Automation Extension
34273429
undetectable - the option to use an undetectable chromedriver
3430+
uc_cdp_events - capture CDP events in "undetectable" mode (Chrome)
34283431
uc_subprocess - use the undetectable chromedriver as a subprocess
34293432
no_sandbox - the option to enable the "No-Sandbox" feature (Chrome)
34303433
disable_gpu - the option to enable Chrome's "Disable GPU" feature
@@ -3522,6 +3525,8 @@ def get_new_driver(
35223525
use_auto_ext = self.use_auto_ext
35233526
if undetectable is None:
35243527
undetectable = self.undetectable
3528+
if uc_cdp_events is None:
3529+
uc_cdp_events = self.uc_cdp_events
35253530
if uc_subprocess is None:
35263531
uc_subprocess = self.uc_subprocess
35273532
if no_sandbox is None:
@@ -3609,6 +3614,7 @@ def get_new_driver(
36093614
enable_sync=enable_sync,
36103615
use_auto_ext=use_auto_ext,
36113616
undetectable=undetectable,
3617+
uc_cdp_events=uc_cdp_events,
36123618
uc_subprocess=uc_subprocess,
36133619
no_sandbox=no_sandbox,
36143620
disable_gpu=disable_gpu,
@@ -13305,6 +13311,7 @@ def setUp(self, masterqa_mode=False):
1330513311
self.enable_sync = sb_config.enable_sync
1330613312
self.use_auto_ext = sb_config.use_auto_ext
1330713313
self.undetectable = sb_config.undetectable
13314+
self.uc_cdp_events = sb_config.uc_cdp_events
1330813315
self.uc_subprocess = sb_config.uc_subprocess
1330913316
self.no_sandbox = sb_config.no_sandbox
1331013317
self.disable_gpu = sb_config.disable_gpu
@@ -13619,6 +13626,7 @@ def setUp(self, masterqa_mode=False):
1361913626
enable_sync=self.enable_sync,
1362013627
use_auto_ext=self.use_auto_ext,
1362113628
undetectable=self.undetectable,
13629+
uc_cdp_events=self.uc_cdp_events,
1362213630
uc_subprocess=self.uc_subprocess,
1362313631
no_sandbox=self.no_sandbox,
1362413632
disable_gpu=self.disable_gpu,

seleniumbase/plugins/driver_manager.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def Driver(
7474
enable_sync=None, # Enable "Chrome Sync" on websites.
7575
use_auto_ext=None, # Use Chrome's automation extension.
7676
undetectable=None, # Use undetected-chromedriver to evade bot-detection.
77+
uc_cdp_events=None, # Capture CDP events in undetected-chromedriver mode.
7778
uc_subprocess=None, # Use undetected-chromedriver as a subprocess.
7879
no_sandbox=None, # (DEPRECATED) - "--no-sandbox" is always used now.
7980
disable_gpu=None, # (DEPRECATED) - GPU is disabled if no "swiftshader".
@@ -102,6 +103,7 @@ def Driver(
102103
d_p_r=None, # Set device pixel ratio
103104
uc=None, # Shortcut / Duplicate of "undetectable".
104105
undetected=None, # Shortcut / Duplicate of "undetectable".
106+
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
105107
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
106108
wire=None, # Shortcut / Duplicate of "use_wire".
107109
pls=None, # Shortcut / Duplicate of "page_load_strategy".
@@ -271,7 +273,15 @@ def Driver(
271273
enable_ws = True
272274
else:
273275
enable_ws = False
274-
if undetectable or undetected or uc or uc_subprocess or uc_sub:
276+
if (
277+
undetectable
278+
or undetected
279+
or uc
280+
or uc_cdp_events
281+
or uc_cdp
282+
or uc_subprocess
283+
or uc_sub
284+
):
275285
undetectable = True
276286
if (
277287
(undetectable or undetected or uc)
@@ -283,6 +293,9 @@ def Driver(
283293
"--undetectable" in sys_argv
284294
or "--undetected" in sys_argv
285295
or "--uc" in sys_argv
296+
or "--uc-cdp-events" in sys_argv
297+
or "--uc_cdp_events" in sys_argv
298+
or "--uc-cdp" in sys_argv
286299
or "--uc-subprocess" in sys_argv
287300
or "--uc_subprocess" in sys_argv
288301
or "--uc-sub" in sys_argv
@@ -399,6 +412,7 @@ def Driver(
399412
enable_sync=enable_sync,
400413
use_auto_ext=use_auto_ext,
401414
undetectable=undetectable,
415+
uc_cdp_events=uc_cdp_events,
402416
uc_subprocess=uc_subprocess,
403417
no_sandbox=no_sandbox,
404418
disable_gpu=disable_gpu,

seleniumbase/plugins/pytest_plugin.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ def pytest_addoption(parser):
8686
--enable-ws (Enable Web Security on Chromium-based browsers.)
8787
--enable-sync (Enable "Chrome Sync" on websites.)
8888
--uc | --undetected (Use undetected-chromedriver to evade bot-detection.)
89+
--uc-cdp-events (Capture CDP events when running in "--undetected" mode.)
8990
--remote-debug (Sync to Chrome Remote Debugger chrome://inspect/#devices)
9091
--final-debug (Enter Debug Mode after each test ends. Don't use with CI!)
9192
--dashboard (Enable the SeleniumBase Dashboard. Saved at: dashboard.html)
@@ -909,6 +910,22 @@ def pytest_addoption(parser):
909910
to websites that use anti-bot services to block
910911
automation tools from navigating them freely.""",
911912
)
913+
parser.addoption(
914+
"--uc_cdp_events",
915+
"--uc-cdp-events",
916+
"--uc-cdp", # For capturing CDP events during UC Mode
917+
action="store_true",
918+
dest="uc_cdp_events",
919+
default=None,
920+
help="""Captures CDP events during Undetectable Mode runs.
921+
Then you can add a listener to perform actions on
922+
received data, such as printing it to the console:
923+
from pprint import pformat
924+
self.driver.add_cdp_listener(
925+
"*", lambda data: print(pformat(data))
926+
)
927+
self.open(URL)""",
928+
)
912929
parser.addoption(
913930
"--uc_subprocess",
914931
"--uc-subprocess",
@@ -1300,6 +1317,9 @@ def pytest_addoption(parser):
13001317
"--undetected" in sys_argv
13011318
or "--undetectable" in sys_argv
13021319
or "--uc" in sys_argv
1320+
or "--uc-cdp-events" in sys_argv
1321+
or "--uc_cdp_events" in sys_argv
1322+
or "--uc-cdp" in sys_argv
13031323
or "--uc-subprocess" in sys_argv
13041324
or "--uc_subprocess" in sys_argv
13051325
or "--uc-sub" in sys_argv
@@ -1436,6 +1456,9 @@ def pytest_configure(config):
14361456
sb_config.enable_sync = config.getoption("enable_sync")
14371457
sb_config.use_auto_ext = config.getoption("use_auto_ext")
14381458
sb_config.undetectable = config.getoption("undetectable")
1459+
sb_config.uc_cdp_events = config.getoption("uc_cdp_events")
1460+
if sb_config.uc_cdp_events and not sb_config.undetectable:
1461+
sb_config.undetectable = True
14391462
sb_config.uc_subprocess = config.getoption("uc_subprocess")
14401463
if sb_config.uc_subprocess and not sb_config.undetectable:
14411464
sb_config.undetectable = True

seleniumbase/plugins/sb_manager.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def SB(
4545
enable_sync=None, # Enable "Chrome Sync" on websites.
4646
use_auto_ext=None, # Use Chrome's automation extension.
4747
undetectable=None, # Use undetected-chromedriver to evade bot-detection.
48+
uc_cdp_events=None, # Capture CDP events in undetected-chromedriver mode.
4849
uc_subprocess=None, # Use undetected-chromedriver as a subprocess.
4950
incognito=None, # Enable Chromium's Incognito mode.
5051
guest_mode=None, # Enable Chromium's Guest mode.
@@ -87,6 +88,7 @@ def SB(
8788
settings_file=None, # A file for overriding default SeleniumBase settings.
8889
uc=None, # Shortcut / Duplicate of "undetectable".
8990
undetected=None, # Shortcut / Duplicate of "undetectable".
91+
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
9092
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
9193
wire=None, # Shortcut / Duplicate of "use_wire".
9294
pls=None, # Shortcut / Duplicate of "page_load_strategy".
@@ -391,7 +393,15 @@ def SB(
391393
else:
392394
enable_ws = False
393395
disable_ws = True
394-
if undetectable or undetected or uc or uc_subprocess or uc_sub:
396+
if (
397+
undetectable
398+
or undetected
399+
or uc
400+
or uc_cdp_events
401+
or uc_cdp
402+
or uc_subprocess
403+
or uc_sub
404+
):
395405
undetectable = True
396406
if (
397407
(undetectable or undetected or uc)
@@ -403,6 +413,9 @@ def SB(
403413
"--undetectable" in sys_argv
404414
or "--undetected" in sys_argv
405415
or "--uc" in sys_argv
416+
or "--uc-cdp-events" in sys_argv
417+
or "--uc_cdp_events" in sys_argv
418+
or "--uc-cdp" in sys_argv
406419
or "--uc-subprocess" in sys_argv
407420
or "--uc_subprocess" in sys_argv
408421
or "--uc-sub" in sys_argv
@@ -600,6 +613,7 @@ def SB(
600613
sb_config.enable_sync = enable_sync
601614
sb_config.use_auto_ext = use_auto_ext
602615
sb_config.undetectable = undetectable
616+
sb_config.uc_cdp_events = uc_cdp_events
603617
sb_config.uc_subprocess = uc_subprocess
604618
sb_config.no_sandbox = None
605619
sb_config.disable_gpu = None
@@ -695,6 +709,7 @@ def SB(
695709
sb.enable_sync = sb_config.enable_sync
696710
sb.use_auto_ext = sb_config.use_auto_ext
697711
sb.undetectable = sb_config.undetectable
712+
sb.uc_cdp_events = sb_config.uc_cdp_events
698713
sb.uc_subprocess = sb_config.uc_subprocess
699714
sb.no_sandbox = sb_config.no_sandbox
700715
sb.disable_gpu = sb_config.disable_gpu

0 commit comments

Comments
 (0)