Merge pull request #3587 from seleniumbase/cdp-mode-patch-40

CDP Mode: Patch 40
This commit is contained in:
Michael Mintz 2025-03-05 13:21:34 -05:00 committed by GitHub
commit 24f4a91dab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 37 additions and 4 deletions

View File

@ -420,8 +420,10 @@ sb.cdp.minimize()
sb.cdp.medimize()
sb.cdp.set_window_rect()
sb.cdp.reset_window_size()
sb.cdp.open_new_window(url=None, switch_to=True)
sb.cdp.switch_to_window(window)
sb.cdp.switch_to_newest_window()
sb.cdp.open_new_tab(url=None, switch_to=True)
sb.cdp.switch_to_tab(tab)
sb.cdp.switch_to_newest_tab()
sb.cdp.close_active_tab()

View File

@ -0,0 +1,15 @@
from seleniumbase import SB
with SB(uc=True) as sb:
sb.activate_cdp_mode()
sb.open("data:text/html,<h1>Page A</h1>")
sb.assert_text("Page A")
sb.open_new_tab()
sb.open("data:text/html,<h1>Page B</h1>")
sb.assert_text("Page B")
sb.switch_to_tab(0)
sb.assert_text("Page A")
sb.assert_text_not_visible("Page B")
sb.switch_to_tab(1)
sb.assert_text("Page B")
sb.assert_text_not_visible("Page A")

View File

@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.35.3"
__version__ = "4.35.4"

View File

@ -682,8 +682,10 @@ def uc_open_with_cdp_mode(driver, url=None):
cdp.gui_hover_element = CDPM.gui_hover_element
cdp.gui_hover_and_click = CDPM.gui_hover_and_click
cdp.internalize_links = CDPM.internalize_links
cdp.open_new_window = CDPM.open_new_window
cdp.switch_to_window = CDPM.switch_to_window
cdp.switch_to_newest_window = CDPM.switch_to_newest_window
cdp.open_new_tab = CDPM.open_new_tab
cdp.switch_to_tab = CDPM.switch_to_tab
cdp.switch_to_newest_tab = CDPM.switch_to_newest_tab
cdp.close_active_tab = CDPM.close_active_tab

View File

@ -106,7 +106,7 @@ class CDPMethods():
driver = self.driver
if hasattr(driver, "cdp_base"):
driver = driver.cdp_base
self.page = self.loop.run_until_complete(driver.get(url))
self.loop.run_until_complete(self.page.get(url))
url_protocol = url.split(":")[0]
safe_url = True
if url_protocol not in ["about", "data", "chrome"]:
@ -1014,12 +1014,22 @@ class CDPMethods():
self.set_window_rect(x, y, width, height)
self.__add_light_pause()
def open_new_window(self, url=None, switch_to=True):
return self.open_new_tab(url=url, switch_to=switch_to)
def switch_to_window(self, window):
self.switch_to_tab(window)
def switch_to_newest_window(self):
self.switch_to_tab(-1)
def open_new_tab(self, url=None, switch_to=True):
if not isinstance(url, str):
url = "about:blank"
self.loop.run_until_complete(self.page.get(url, new_tab=True))
if switch_to:
self.switch_to_newest_tab()
def switch_to_tab(self, tab):
driver = self.driver
if hasattr(driver, "cdp_base"):

View File

@ -3896,6 +3896,9 @@ class BaseCase(unittest.TestCase):
def open_new_window(self, switch_to=True):
"""Opens a new browser tab/window and switches to it by default."""
if self.__is_cdp_swap_needed():
self.cdp.open_new_tab(switch_to=switch_to)
return
self.wait_for_ready_state_complete()
if switch_to:
try:
@ -10339,7 +10342,7 @@ class BaseCase(unittest.TestCase):
timeout = self.__get_new_timeout(timeout)
selector, by = self.__recalculate_selector(selector, by)
if self.__is_cdp_swap_needed():
return self.cdp.wait_for_text(
return self.cdp.wait_for_text_not_visible(
text, selector=selector, timeout=timeout
)
return page_actions.wait_for_text_not_visible(

View File

@ -477,6 +477,7 @@ class Chrome(selenium.webdriver.chrome.webdriver.WebDriver):
with suppress(Exception):
if self.service.is_connectable():
self.stop_client()
time.sleep(0.003)
self.service.stop()
self._is_connected = False

View File

@ -792,7 +792,7 @@ class CookieJar:
connection = self._browser.connection
cookies = await connection.send(cdp.network.get_cookies())
if cookies:
await connection.send(cdp.network.clear_cookies())
await connection.send(cdp.storage.clear_cookies())
class HTTPApi: