From a68f33ce0d883e500bedac8cb2519a3fc6aed37a Mon Sep 17 00:00:00 2001 From: Michael Mintz Date: Mon, 7 Apr 2025 16:26:33 -0400 Subject: [PATCH] Fix CDP Mode issues --- seleniumbase/fixtures/base_case.py | 7 ++++ seleniumbase/undetected/__init__.py | 40 ++++++++++++++++--- .../undetected/cdp_driver/cdp_util.py | 8 +++- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/seleniumbase/fixtures/base_case.py b/seleniumbase/fixtures/base_case.py index 528d1d8a..3e55e5eb 100644 --- a/seleniumbase/fixtures/base_case.py +++ b/seleniumbase/fixtures/base_case.py @@ -4871,6 +4871,13 @@ class BaseCase(unittest.TestCase): def activate_cdp_mode(self, url=None): if hasattr(self.driver, "_is_using_uc") and self.driver._is_using_uc: + if self.__is_cdp_swap_needed(): + return # CDP Mode is already active + if not self.is_connected(): + self.driver.connect() + current_url = self.get_current_url() + if not current_url.startswith(("about", "data", "chrome")): + self.get_new_driver(undetectable=True) self.driver.uc_open_with_cdp_mode(url) else: self.get_new_driver(undetectable=True) diff --git a/seleniumbase/undetected/__init__.py b/seleniumbase/undetected/__init__.py index 1dbbdccc..04a562d7 100644 --- a/seleniumbase/undetected/__init__.py +++ b/seleniumbase/undetected/__init__.py @@ -441,7 +441,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): with suppress(Exception): if self.service.is_connectable(): self.stop_client() - self.service.stop() + try: + self.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + self.service._terminate_process() if isinstance(timeout, str): if timeout.lower() == "breakpoint": breakpoint() # To continue: @@ -466,7 +472,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): self.close() if self.service.is_connectable(): self.stop_client() - self.service.stop() + try: + self.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + self.service._terminate_process() self.service.start() self.start_session() time.sleep(0.003) @@ -482,7 +494,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): if self.service.is_connectable(): self.stop_client() time.sleep(0.003) - self.service.stop() + try: + self.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + self.service._terminate_process() self._is_connected = False def connect(self): @@ -507,7 +525,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): self.close() if self.service.is_connectable(): self.stop_client() - self.service.stop() + try: + self.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + self.service._terminate_process() self.service.start() self.start_session() time.sleep(0.003) @@ -539,7 +563,13 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver): logger.debug("Stopping webdriver service") with suppress(Exception): self.stop_client() - self.service.stop() + try: + self.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + self.service._terminate_process() with suppress(Exception): if self.reactor and isinstance(self.reactor, Reactor): logger.debug("Shutting down Reactor") diff --git a/seleniumbase/undetected/cdp_driver/cdp_util.py b/seleniumbase/undetected/cdp_driver/cdp_util.py index ab5886b6..29a166b1 100644 --- a/seleniumbase/undetected/cdp_driver/cdp_util.py +++ b/seleniumbase/undetected/cdp_driver/cdp_util.py @@ -406,7 +406,13 @@ async def create_from_driver(driver) -> Browser: browser = await start(conf) browser._process_pid = driver.browser_pid # Stop chromedriver binary - driver.service.stop() + try: + driver.service.send_remote_shutdown_command() + except TypeError: + pass + finally: + with suppress(Exception): + driver.service._terminate_process() driver.browser_pid = -1 driver.user_data_dir = None return browser