Skip to content

Commit 6d8744d

Browse files
committed
Update CDP Mode
1 parent c709379 commit 6d8744d

File tree

5 files changed

+42
-10
lines changed

5 files changed

+42
-10
lines changed

seleniumbase/core/browser_launcher.py

+10
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,16 @@ def uc_open_with_cdp_mode(driver, url=None, **kwargs):
519519
import asyncio
520520
from seleniumbase.undetected.cdp_driver import cdp_util
521521

522+
if (
523+
hasattr(driver, "_is_using_cdp")
524+
and driver._is_using_cdp
525+
and hasattr(driver, "cdp")
526+
and driver.cdp
527+
):
528+
# CDP Mode was already initialized
529+
driver.disconnect()
530+
driver.cdp.open(url, **kwargs)
531+
return
522532
current_url = None
523533
try:
524534
current_url = driver.current_url

seleniumbase/fixtures/base_case.py

+12
Original file line numberDiff line numberDiff line change
@@ -3534,12 +3534,24 @@ def set_window_rect(self, x, y, width, height):
35343534

35353535
def set_window_size(self, width, height):
35363536
self.__check_scope()
3537+
if self.__is_cdp_swap_needed():
3538+
position = self.cdp.get_window_position()
3539+
x = position["x"]
3540+
y = position["y"]
3541+
self.cdp.set_window_rect(x, y, width, height)
3542+
return
35373543
self._check_browser()
35383544
self.driver.set_window_size(width, height)
35393545
self.__demo_mode_pause_if_active(tiny=True)
35403546

35413547
def set_window_position(self, x, y):
35423548
self.__check_scope()
3549+
if self.__is_cdp_swap_needed():
3550+
size = self.cdp.get_window_size()
3551+
width = size["width"]
3552+
height = size["height"]
3553+
self.cdp.set_window_rect(x, y, width, height)
3554+
return
35433555
self._check_browser()
35443556
self.driver.set_window_position(x, y)
35453557
self.__demo_mode_pause_if_active(tiny=True)

seleniumbase/plugins/sb_manager.py

+16-7
Original file line numberDiff line numberDiff line change
@@ -1369,14 +1369,23 @@ def SB(
13691369
"%s%s%s%s%s"
13701370
% (c1, left_space, end_text, right_space, cr)
13711371
)
1372-
if undetectable and hasattr(sb, "_drivers_browser_map"):
1372+
python3_12_or_newer = (sys.version_info >= (3, 12))
1373+
if undetectable:
13731374
import asyncio
1374-
for driver in sb._drivers_browser_map.keys():
1375-
if hasattr(driver, "cdp") and driver.cdp:
1376-
asyncio.set_event_loop(driver.cdp.loop)
1377-
tasks = [tab.aclose() for tab in driver.cdp.get_tabs()]
1378-
tasks.append(driver.cdp.driver.connection.aclose())
1379-
driver.cdp.loop.run_until_complete(asyncio.gather(*tasks))
1375+
if not python3_12_or_newer and hasattr(sb_config, "_cdp_aclose"):
1376+
with suppress(Exception):
1377+
loop = asyncio.get_event_loop()
1378+
asyncio.set_event_loop(loop)
1379+
loop.run_until_complete(sb_config._cdp_aclose())
1380+
if python3_12_or_newer and hasattr(sb, "_drivers_browser_map"):
1381+
for driver in sb._drivers_browser_map.keys():
1382+
if hasattr(driver, "cdp") and driver.cdp:
1383+
asyncio.set_event_loop(driver.cdp.loop)
1384+
tasks = [tab.aclose() for tab in driver.cdp.get_tabs()]
1385+
tasks.append(driver.cdp.driver.connection.aclose())
1386+
driver.cdp.loop.run_until_complete(
1387+
asyncio.gather(*tasks)
1388+
)
13801389
driver.cdp.loop.close()
13811390
gc.collect()
13821391
if test and test_name and not test_passed and raise_test_failure:

seleniumbase/undetected/__init__.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -523,9 +523,7 @@ def connect(self):
523523
with suppress(Exception):
524524
for window_handle in self.window_handles:
525525
self.switch_to.window(window_handle)
526-
if self.current_url.startswith(
527-
"chrome-extension://"
528-
):
526+
if self.current_url.startswith("chrome-extension://"):
529527
# https://issues.chromium.org/issues/396611138
530528
# (Remove the Linux conditional when resolved)
531529
# (So that close() is always called)

seleniumbase/undetected/cdp_driver/connection.py

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020
import websockets
2121
from websockets.protocol import State
22+
from seleniumbase import config as sb_config
2223
from . import cdp_util as util
2324
import mycdp as cdp
2425
import mycdp.network
@@ -270,6 +271,7 @@ async def aopen(self, **kw):
270271
max_size=MAX_SIZE,
271272
)
272273
self.listener = Listener(self)
274+
sb_config._cdp_aclose = self.aclose
273275
except (Exception,) as e:
274276
logger.debug("Exception during opening of websocket: %s", e)
275277
if self.listener:
@@ -444,6 +446,7 @@ async def send(
444446
if not _is_update:
445447
await self._register_handlers()
446448
await self.websocket.send(tx.message)
449+
sb_config._cdp_aclose = self.aclose
447450
try:
448451
return await tx
449452
except ProtocolException as e:

0 commit comments

Comments
 (0)