Add option to capture CDP events in UC Mode

This commit is contained in:
Michael Mintz 2023-01-19 00:02:04 -05:00
parent 0862fbcd3a
commit 0a4808fafb
9 changed files with 106 additions and 3 deletions

View File

@ -66,6 +66,7 @@ if pure_python:
sb.enable_sync = False
sb.use_auto_ext = False
sb.undetectable = False
sb.uc_cdp_events = False
sb.uc_subprocess = False
sb.no_sandbox = False
sb.disable_js = False

View File

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

View File

@ -72,6 +72,7 @@ behave -D agent="User Agent String" -D demo
-D enable-ws (Enable Web Security on Chromium-based browsers.)
-D enable-sync (Enable "Chrome Sync".)
-D uc | -D undetected (Use undetected-chromedriver to evade bot-detection)
-D uc-cdp-events (Capture CDP events when running in "--undetected" mode.)
-D remote-debug (Sync to Chrome Remote Debugger chrome://inspect/#devices)
-D dashboard (Enable the SeleniumBase Dashboard. Saved at: dashboard.html)
-D dash-title=STRING (Set the title shown for the generated dashboard.)
@ -171,6 +172,7 @@ def get_configured_sb(context):
sb.enable_sync = False
sb.use_auto_ext = False
sb.undetectable = False
sb.uc_cdp_events = False
sb.uc_subprocess = False
sb.no_sandbox = False
sb.disable_gpu = False
@ -509,6 +511,11 @@ def get_configured_sb(context):
if low_key in ["undetected", "undetectable", "uc"]:
sb.undetectable = True
continue
# Handle: -D uc-cdp-events / uc_cdp_events / uc-cdp
if low_key in ["uc-cdp-events", "uc_cdp_events", "uc-cdp"]:
sb.uc_cdp_events = True
sb.undetectable = True
continue
# Handle: -D uc-subprocess / uc_subprocess / uc-sub
if low_key in ["uc-subprocess", "uc_subprocess", "uc-sub"]:
sb.uc_subprocess = True

View File

@ -385,6 +385,7 @@ def _set_chrome_options(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -943,6 +944,7 @@ def get_driver(
enable_sync=False,
use_auto_ext=False,
undetectable=False,
uc_cdp_events=False,
uc_subprocess=False,
no_sandbox=False,
disable_gpu=False,
@ -981,7 +983,7 @@ def get_driver(
if headless2 and browser_name == constants.Browser.FIREFOX:
headless2 = False # Only for Chromium
headless = True
if uc_subprocess and not undetectable:
if (uc_cdp_events or uc_subprocess) and not undetectable:
undetectable = True
if is_using_uc(undetectable, browser_name) and mobile_emulator:
mobile_emulator = False
@ -1114,6 +1116,7 @@ def get_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -1162,6 +1165,7 @@ def get_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -1214,6 +1218,7 @@ def get_remote_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -1331,6 +1336,7 @@ def get_remote_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -1562,6 +1568,7 @@ def get_remote_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -1683,6 +1690,7 @@ def get_local_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -2373,6 +2381,7 @@ def get_local_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -2425,6 +2434,7 @@ def get_local_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,
@ -2707,6 +2717,7 @@ def get_local_driver(
constants.MultiBrowser.DRIVER_FIXING_LOCK
)
with uc_lock: # Avoid multithreaded issues
cdp_events = uc_cdp_events
try:
uc_path = None
if os.path.exists(LOCAL_UC_DRIVER):
@ -2716,6 +2727,7 @@ def get_local_driver(
options=chrome_options,
user_data_dir=user_data_dir,
driver_executable_path=uc_path,
enable_cdp_events=cdp_events,
headless=False, # Xvfb needed!
version_main=uc_chrome_version,
use_subprocess=True, # Always!
@ -2734,6 +2746,7 @@ def get_local_driver(
options=chrome_options,
user_data_dir=user_data_dir,
driver_executable_path=uc_path,
enable_cdp_events=cdp_events,
headless=False, # Xvfb needed!
version_main=uc_chrome_version,
use_subprocess=True, # Always!
@ -2828,6 +2841,7 @@ def get_local_driver(
enable_sync,
use_auto_ext,
undetectable,
uc_cdp_events,
uc_subprocess,
no_sandbox,
disable_gpu,

View File

@ -2603,6 +2603,7 @@ class BaseCase(unittest.TestCase):
self.__check_scope()
if (
not self.undetectable
or self.uc_cdp_events
or self.__uc_frame_layer > 0
or not hasattr(element, "uc_click")
):
@ -3374,6 +3375,7 @@ class BaseCase(unittest.TestCase):
enable_sync=None,
use_auto_ext=None,
undetectable=None,
uc_cdp_events=None,
uc_subprocess=None,
no_sandbox=None,
disable_gpu=None,
@ -3425,6 +3427,7 @@ class BaseCase(unittest.TestCase):
enable_sync - the option to enable the Chrome Sync feature (Chrome)
use_auto_ext - the option to enable Chrome's Automation Extension
undetectable - the option to use an undetectable chromedriver
uc_cdp_events - capture CDP events in "undetectable" mode (Chrome)
uc_subprocess - use the undetectable chromedriver as a subprocess
no_sandbox - the option to enable the "No-Sandbox" feature (Chrome)
disable_gpu - the option to enable Chrome's "Disable GPU" feature
@ -3522,6 +3525,8 @@ class BaseCase(unittest.TestCase):
use_auto_ext = self.use_auto_ext
if undetectable is None:
undetectable = self.undetectable
if uc_cdp_events is None:
uc_cdp_events = self.uc_cdp_events
if uc_subprocess is None:
uc_subprocess = self.uc_subprocess
if no_sandbox is None:
@ -3609,6 +3614,7 @@ class BaseCase(unittest.TestCase):
enable_sync=enable_sync,
use_auto_ext=use_auto_ext,
undetectable=undetectable,
uc_cdp_events=uc_cdp_events,
uc_subprocess=uc_subprocess,
no_sandbox=no_sandbox,
disable_gpu=disable_gpu,
@ -13305,6 +13311,7 @@ class BaseCase(unittest.TestCase):
self.enable_sync = sb_config.enable_sync
self.use_auto_ext = sb_config.use_auto_ext
self.undetectable = sb_config.undetectable
self.uc_cdp_events = sb_config.uc_cdp_events
self.uc_subprocess = sb_config.uc_subprocess
self.no_sandbox = sb_config.no_sandbox
self.disable_gpu = sb_config.disable_gpu
@ -13619,6 +13626,7 @@ class BaseCase(unittest.TestCase):
enable_sync=self.enable_sync,
use_auto_ext=self.use_auto_ext,
undetectable=self.undetectable,
uc_cdp_events=self.uc_cdp_events,
uc_subprocess=self.uc_subprocess,
no_sandbox=self.no_sandbox,
disable_gpu=self.disable_gpu,

View File

@ -74,6 +74,7 @@ def Driver(
enable_sync=None, # Enable "Chrome Sync" on websites.
use_auto_ext=None, # Use Chrome's automation extension.
undetectable=None, # Use undetected-chromedriver to evade bot-detection.
uc_cdp_events=None, # Capture CDP events in undetected-chromedriver mode.
uc_subprocess=None, # Use undetected-chromedriver as a subprocess.
no_sandbox=None, # (DEPRECATED) - "--no-sandbox" is always used now.
disable_gpu=None, # (DEPRECATED) - GPU is disabled if no "swiftshader".
@ -102,6 +103,7 @@ def Driver(
d_p_r=None, # Set device pixel ratio
uc=None, # Shortcut / Duplicate of "undetectable".
undetected=None, # Shortcut / Duplicate of "undetectable".
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
wire=None, # Shortcut / Duplicate of "use_wire".
pls=None, # Shortcut / Duplicate of "page_load_strategy".
@ -271,7 +273,15 @@ def Driver(
enable_ws = True
else:
enable_ws = False
if undetectable or undetected or uc or uc_subprocess or uc_sub:
if (
undetectable
or undetected
or uc
or uc_cdp_events
or uc_cdp
or uc_subprocess
or uc_sub
):
undetectable = True
if (
(undetectable or undetected or uc)
@ -283,6 +293,9 @@ def Driver(
"--undetectable" in sys_argv
or "--undetected" in sys_argv
or "--uc" in sys_argv
or "--uc-cdp-events" in sys_argv
or "--uc_cdp_events" in sys_argv
or "--uc-cdp" in sys_argv
or "--uc-subprocess" in sys_argv
or "--uc_subprocess" in sys_argv
or "--uc-sub" in sys_argv
@ -399,6 +412,7 @@ def Driver(
enable_sync=enable_sync,
use_auto_ext=use_auto_ext,
undetectable=undetectable,
uc_cdp_events=uc_cdp_events,
uc_subprocess=uc_subprocess,
no_sandbox=no_sandbox,
disable_gpu=disable_gpu,

View File

@ -86,6 +86,7 @@ def pytest_addoption(parser):
--enable-ws (Enable Web Security on Chromium-based browsers.)
--enable-sync (Enable "Chrome Sync" on websites.)
--uc | --undetected (Use undetected-chromedriver to evade bot-detection.)
--uc-cdp-events (Capture CDP events when running in "--undetected" mode.)
--remote-debug (Sync to Chrome Remote Debugger chrome://inspect/#devices)
--final-debug (Enter Debug Mode after each test ends. Don't use with CI!)
--dashboard (Enable the SeleniumBase Dashboard. Saved at: dashboard.html)
@ -909,6 +910,22 @@ def pytest_addoption(parser):
to websites that use anti-bot services to block
automation tools from navigating them freely.""",
)
parser.addoption(
"--uc_cdp_events",
"--uc-cdp-events",
"--uc-cdp", # For capturing CDP events during UC Mode
action="store_true",
dest="uc_cdp_events",
default=None,
help="""Captures CDP events during Undetectable Mode runs.
Then you can add a listener to perform actions on
received data, such as printing it to the console:
from pprint import pformat
self.driver.add_cdp_listener(
"*", lambda data: print(pformat(data))
)
self.open(URL)""",
)
parser.addoption(
"--uc_subprocess",
"--uc-subprocess",
@ -1300,6 +1317,9 @@ def pytest_addoption(parser):
"--undetected" in sys_argv
or "--undetectable" in sys_argv
or "--uc" in sys_argv
or "--uc-cdp-events" in sys_argv
or "--uc_cdp_events" in sys_argv
or "--uc-cdp" in sys_argv
or "--uc-subprocess" in sys_argv
or "--uc_subprocess" in sys_argv
or "--uc-sub" in sys_argv
@ -1436,6 +1456,9 @@ def pytest_configure(config):
sb_config.enable_sync = config.getoption("enable_sync")
sb_config.use_auto_ext = config.getoption("use_auto_ext")
sb_config.undetectable = config.getoption("undetectable")
sb_config.uc_cdp_events = config.getoption("uc_cdp_events")
if sb_config.uc_cdp_events and not sb_config.undetectable:
sb_config.undetectable = True
sb_config.uc_subprocess = config.getoption("uc_subprocess")
if sb_config.uc_subprocess and not sb_config.undetectable:
sb_config.undetectable = True

View File

@ -45,6 +45,7 @@ def SB(
enable_sync=None, # Enable "Chrome Sync" on websites.
use_auto_ext=None, # Use Chrome's automation extension.
undetectable=None, # Use undetected-chromedriver to evade bot-detection.
uc_cdp_events=None, # Capture CDP events in undetected-chromedriver mode.
uc_subprocess=None, # Use undetected-chromedriver as a subprocess.
incognito=None, # Enable Chromium's Incognito mode.
guest_mode=None, # Enable Chromium's Guest mode.
@ -87,6 +88,7 @@ def SB(
settings_file=None, # A file for overriding default SeleniumBase settings.
uc=None, # Shortcut / Duplicate of "undetectable".
undetected=None, # Shortcut / Duplicate of "undetectable".
uc_cdp=None, # Shortcut / Duplicate of "uc_cdp_events".
uc_sub=None, # Shortcut / Duplicate of "uc_subprocess".
wire=None, # Shortcut / Duplicate of "use_wire".
pls=None, # Shortcut / Duplicate of "page_load_strategy".
@ -391,7 +393,15 @@ def SB(
else:
enable_ws = False
disable_ws = True
if undetectable or undetected or uc or uc_subprocess or uc_sub:
if (
undetectable
or undetected
or uc
or uc_cdp_events
or uc_cdp
or uc_subprocess
or uc_sub
):
undetectable = True
if (
(undetectable or undetected or uc)
@ -403,6 +413,9 @@ def SB(
"--undetectable" in sys_argv
or "--undetected" in sys_argv
or "--uc" in sys_argv
or "--uc-cdp-events" in sys_argv
or "--uc_cdp_events" in sys_argv
or "--uc-cdp" in sys_argv
or "--uc-subprocess" in sys_argv
or "--uc_subprocess" in sys_argv
or "--uc-sub" in sys_argv
@ -600,6 +613,7 @@ def SB(
sb_config.enable_sync = enable_sync
sb_config.use_auto_ext = use_auto_ext
sb_config.undetectable = undetectable
sb_config.uc_cdp_events = uc_cdp_events
sb_config.uc_subprocess = uc_subprocess
sb_config.no_sandbox = None
sb_config.disable_gpu = None
@ -695,6 +709,7 @@ def SB(
sb.enable_sync = sb_config.enable_sync
sb.use_auto_ext = sb_config.use_auto_ext
sb.undetectable = sb_config.undetectable
sb.uc_cdp_events = sb_config.uc_cdp_events
sb.uc_subprocess = sb_config.uc_subprocess
sb.no_sandbox = sb_config.no_sandbox
sb.disable_gpu = sb_config.disable_gpu

View File

@ -68,6 +68,7 @@ class SeleniumBrowser(Plugin):
--enable-ws (Enable Web Security on Chromium-based browsers.)
--enable-sync (Enable "Chrome Sync" on websites.)
--uc | --undetected (Use undetected-chromedriver to evade bot-detection.)
--uc-cdp-events (Capture CDP events when running in "--undetected" mode.)
--remote-debug (Sync to Chrome Remote Debugger chrome://inspect/#devices)
--final-debug (Enter Debug Mode after each test ends. Don't use with CI!)
--enable-3d-apis (Enables WebGL and 3D APIs.)
@ -666,6 +667,22 @@ class SeleniumBrowser(Plugin):
to websites that use anti-bot services to block
automation tools from navigating them freely.""",
)
parser.addoption(
"--uc_cdp_events",
"--uc-cdp-events",
"--uc-cdp", # For capturing CDP events during UC Mode
action="store_true",
dest="uc_cdp_events",
default=None,
help="""Captures CDP events during Undetectable Mode runs.
Then you can add a listener to perform actions on
received data, such as printing it to the console:
from pprint import pformat
self.driver.add_cdp_listener(
"*", lambda data: print(pformat(data))
)
self.open(URL)""",
)
parser.add_option(
"--uc_subprocess",
"--uc-subprocess",
@ -1061,6 +1078,9 @@ class SeleniumBrowser(Plugin):
test.test.enable_sync = self.options.enable_sync
test.test.use_auto_ext = self.options.use_auto_ext
test.test.undetectable = self.options.undetectable
test.test.uc_cdp_events = self.options.uc_cdp_events
if test.test.uc_cdp_events and not test.test.undetectable:
test.test.undetectable = True
test.test.uc_subprocess = self.options.uc_subprocess
if test.test.uc_subprocess and not test.test.undetectable:
test.test.undetectable = True