Skip to content

Commit cc1073a

Browse files
committed
Update CDP Mode
1 parent aa0b70a commit cc1073a

File tree

7 files changed

+72
-17
lines changed

7 files changed

+72
-17
lines changed

examples/cdp_mode/ReadMe.md

+7
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,13 @@ sb.cdp.minimize()
420420
sb.cdp.medimize()
421421
sb.cdp.set_window_rect()
422422
sb.cdp.reset_window_size()
423+
sb.cdp.switch_to_window(window)
424+
sb.cdp.switch_to_newest_window()
425+
sb.cdp.switch_to_tab(tab)
426+
sb.cdp.switch_to_newest_tab()
427+
sb.cdp.close_active_tab()
428+
sb.cdp.get_active_tab()
429+
sb.cdp.get_tabs()
423430
sb.cdp.get_window()
424431
sb.cdp.get_text(selector)
425432
sb.cdp.get_title()

seleniumbase/core/browser_launcher.py

+8
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,13 @@ def uc_open_with_cdp_mode(driver, url=None):
682682
cdp.gui_hover_element = CDPM.gui_hover_element
683683
cdp.gui_hover_and_click = CDPM.gui_hover_and_click
684684
cdp.internalize_links = CDPM.internalize_links
685+
cdp.switch_to_window = CDPM.switch_to_window
686+
cdp.switch_to_newest_window = CDPM.switch_to_newest_window
687+
cdp.switch_to_tab = CDPM.switch_to_tab
688+
cdp.switch_to_newest_tab = CDPM.switch_to_newest_tab
689+
cdp.close_active_tab = CDPM.close_active_tab
690+
cdp.get_active_tab = CDPM.get_active_tab
691+
cdp.get_tabs = CDPM.get_tabs
685692
cdp.get_window = CDPM.get_window
686693
cdp.get_element_attributes = CDPM.get_element_attributes
687694
cdp.get_element_attribute = CDPM.get_element_attribute
@@ -2033,6 +2040,7 @@ def _set_chrome_options(
20332040
prefs["download.default_directory"] = downloads_path
20342041
prefs["download.directory_upgrade"] = True
20352042
prefs["download.prompt_for_download"] = False
2043+
prefs["download_bubble.partial_view_enabled"] = False
20362044
prefs["credentials_enable_service"] = False
20372045
prefs["local_discovery.notifications_enabled"] = False
20382046
prefs["safebrowsing.enabled"] = False # Prevent PW "data breach" pop-ups

seleniumbase/core/sb_cdp.py

+46-9
Original file line numberDiff line numberDiff line change
@@ -1014,10 +1014,51 @@ def reset_window_size(self):
10141014
self.set_window_rect(x, y, width, height)
10151015
self.__add_light_pause()
10161016

1017+
def switch_to_window(self, window):
1018+
self.switch_to_tab(window)
1019+
1020+
def switch_to_newest_window(self):
1021+
self.switch_to_tab(-1)
1022+
1023+
def switch_to_tab(self, tab):
1024+
driver = self.driver
1025+
if hasattr(driver, "cdp_base"):
1026+
driver = driver.cdp_base
1027+
if isinstance(tab, int):
1028+
self.page = driver.tabs[tab]
1029+
elif isinstance(tab, cdp_util.Tab):
1030+
self.page = tab
1031+
else:
1032+
raise Exception("`tab` must be an int or a Tab type!")
1033+
self.bring_active_window_to_front()
1034+
1035+
def switch_to_newest_tab(self):
1036+
self.switch_to_tab(-1)
1037+
1038+
def close_active_tab(self):
1039+
"""Close the active tab.
1040+
The active tab is the one currenly controlled by CDP.
1041+
The active tab MIGHT NOT be the currently visible tab!
1042+
(If a page opens a new tab, the new tab WON'T be active)
1043+
To switch the active tab, call: sb.switch_to_tab(tab)"""
1044+
return self.loop.run_until_complete(self.page.close())
1045+
1046+
def get_active_tab(self):
1047+
"""Return the active tab.
1048+
The active tab is the one currenly controlled by CDP.
1049+
The active tab MIGHT NOT be the currently visible tab!
1050+
(If a page opens a new tab, the new tab WON'T be active)
1051+
To switch the active tab, call: sb.switch_to_tab(tab)"""
1052+
return self.page
1053+
1054+
def get_tabs(self):
1055+
driver = self.driver
1056+
if hasattr(driver, "cdp_base"):
1057+
driver = driver.cdp_base
1058+
return driver.tabs
1059+
10171060
def get_window(self):
1018-
return self.loop.run_until_complete(
1019-
self.page.get_window()
1020-
)
1061+
return self.loop.run_until_complete(self.page.get_window())
10211062

10221063
def get_text(self, selector):
10231064
return self.find_element(selector).text_all
@@ -1211,14 +1252,10 @@ def get_gui_element_center(self, selector, timeout=None):
12111252
return ((e_x + e_width / 2.0) + 0.5, (e_y + e_height / 2.0) + 0.5)
12121253

12131254
def get_document(self):
1214-
return self.loop.run_until_complete(
1215-
self.page.get_document()
1216-
)
1255+
return self.loop.run_until_complete(self.page.get_document())
12171256

12181257
def get_flattened_document(self):
1219-
return self.loop.run_until_complete(
1220-
self.page.get_flattened_document()
1221-
)
1258+
return self.loop.run_until_complete(self.page.get_flattened_document())
12221259

12231260
def get_element_attributes(self, selector):
12241261
selector = self.__convert_to_css_if_xpath(selector)

seleniumbase/fixtures/base_case.py

+3
Original file line numberDiff line numberDiff line change
@@ -3918,6 +3918,9 @@ def switch_to_window(self, window, timeout=None):
39183918
timeout = settings.SMALL_TIMEOUT
39193919
if self.timeout_multiplier and timeout == settings.SMALL_TIMEOUT:
39203920
timeout = self.__get_new_timeout(timeout)
3921+
if self.__is_cdp_swap_needed() and not isinstance(window, str):
3922+
self.cdp.switch_to_tab(window)
3923+
return
39213924
page_actions.switch_to_window(self.driver, window, timeout)
39223925

39233926
def switch_to_default_window(self):

seleniumbase/undetected/cdp_driver/browser.py

+6-7
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ async def get_all(
660660
break
661661
else:
662662
connection = self._browser.connection
663-
cookies = await connection.send(cdp.storage.get_cookies())
663+
cookies = await connection.send(cdp.network.get_cookies())
664664
if requests_cookie_format:
665665
import requests.cookies
666666

@@ -690,8 +690,7 @@ async def set_all(self, cookies: List[cdp.network.CookieParam]):
690690
break
691691
else:
692692
connection = self._browser.connection
693-
cookies = await connection.send(cdp.storage.get_cookies())
694-
await connection.send(cdp.storage.set_cookies(cookies))
693+
await connection.send(cdp.network.set_cookies(cookies))
695694

696695
async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
697696
"""
@@ -718,7 +717,7 @@ async def save(self, file: PathLike = ".session.dat", pattern: str = ".*"):
718717
break
719718
else:
720719
connection = self._browser.connection
721-
cookies = await connection.send(cdp.storage.get_cookies())
720+
cookies = await connection.send(cdp.network.get_cookies())
722721
# if not connection:
723722
# return
724723
# if not connection.websocket:
@@ -776,7 +775,7 @@ async def load(self, file: PathLike = ".session.dat", pattern: str = ".*"):
776775
cookie.value,
777776
)
778777
break
779-
await connection.send(cdp.storage.set_cookies(included_cookies))
778+
await connection.send(cdp.network.set_cookies(included_cookies))
780779

781780
async def clear(self):
782781
"""
@@ -791,9 +790,9 @@ async def clear(self):
791790
break
792791
else:
793792
connection = self._browser.connection
794-
cookies = await connection.send(cdp.storage.get_cookies())
793+
cookies = await connection.send(cdp.network.get_cookies())
795794
if cookies:
796-
await connection.send(cdp.storage.clear_cookies())
795+
await connection.send(cdp.network.clear_cookies())
797796

798797

799798
class HTTPApi:

seleniumbase/undetected/cdp_driver/connection.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ async def update_target(self):
382382
async def send(
383383
self,
384384
cdp_obj: Generator[dict[str, Any], dict[str, Any], Any],
385-
_is_update=False,
385+
_is_update=True,
386386
) -> Any:
387387
"""
388388
Send a protocol command.

seleniumbase/undetected/cdp_driver/tab.py

+1
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ async def close(self):
852852
await self.send(
853853
cdp.target.close_target(target_id=self.target.target_id)
854854
)
855+
await asyncio.sleep(0.1)
855856

856857
async def get_window(self) -> Tuple[
857858
cdp.browser.WindowID, cdp.browser.Bounds

0 commit comments

Comments
 (0)