Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDP Mode: Patch 40 #3587

Merged
merged 4 commits into from
Mar 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/cdp_mode/ReadMe.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions examples/cdp_mode/raw_tab_switching.py
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion seleniumbase/__version__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# seleniumbase package
__version__ = "4.35.3"
__version__ = "4.35.4"
2 changes: 2 additions & 0 deletions seleniumbase/core/browser_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion seleniumbase/core/sb_cdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get(self, url):
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"]:
Expand Down Expand Up @@ -1014,12 +1014,22 @@ def reset_window_size(self):
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"):
Expand Down
5 changes: 4 additions & 1 deletion seleniumbase/fixtures/base_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -3896,6 +3896,9 @@ def set_content_to_parent_frame(self):

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:
Expand Down Expand Up @@ -10339,7 +10342,7 @@ def wait_for_text_not_visible(
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(
Expand Down
1 change: 1 addition & 0 deletions seleniumbase/undetected/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,7 @@ def disconnect(self):
with suppress(Exception):
if self.service.is_connectable():
self.stop_client()
time.sleep(0.003)
self.service.stop()
self._is_connected = False

Expand Down
2 changes: 1 addition & 1 deletion seleniumbase/undetected/cdp_driver/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ async def clear(self):
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:
Expand Down