From a6037e960ebdf564932825d1e454af2162f5c001 Mon Sep 17 00:00:00 2001 From: g1879 Date: Sat, 11 Nov 2023 19:33:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4to=5Ftab()=E3=80=81to=5Fmain?= =?UTF-8?q?=5Ftab()=E3=80=81main=5Ftab=EF=BC=9Bnew=5Ftab()=E5=88=A0?= =?UTF-8?q?=E9=99=A4switch=5Fto=E5=8F=82=E6=95=B0=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0new=5Fwindow=E3=80=81background=E3=80=81new=5Fcontext?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_page.py | 99 ++++++--------------------- DrissionPage/_pages/chromium_page.pyi | 18 ++--- 2 files changed, 25 insertions(+), 92 deletions(-) diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index ae06dd4..2732bbe 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -85,7 +85,6 @@ class ChromiumPage(ChromiumBase): def _page_init(self): """浏览器相关设置""" self._rect = None - self._main_tab = self.tab_id self._browser.connect_to_page() @property @@ -103,10 +102,6 @@ class ChromiumPage(ChromiumBase): """返回所有标签页id组成的列表""" return self.browser.tabs - @property - def main_tab(self): - return self._main_tab - @property def latest_tab(self): """返回最新的标签页id,最新标签页指最后创建或最后被激活的""" @@ -154,75 +149,31 @@ class ChromiumPage(ChromiumBase): """ return self._browser.find_tabs(title, url, tab_type, single) - def _new_tab(self, url=None, switch_to=False): - """新建一个标签页,该标签页在最后面 + def new_tab(self, url=None, new_window=False, background=False, new_context=False): + """新建一个标签页 :param url: 新标签页跳转到的网址 - :param switch_to: 新建标签页后是否把焦点移过去 - :return: 新标签页的id + :param new_window: 是否在新窗口打开标签页 + :param background: 是否不激活新标签页,如new_window为True则无效 + :param new_context: 是否创建新的上下文 + :return: 新标签页对象 """ - if switch_to: - tid = self.run_cdp('Target.createTarget', url='')['targetId'] - self._to_tab(tid, read_doc=False) - if url: - self.get(url) + bid = None + if new_context: + bid = self.browser.run_cdp('Target.createBrowserContext', **kwargs)['browserContextId'] - elif url: - tid = self.run_cdp('Target.createTarget', url=url)['targetId'] + kwargs = {'url': ''} + if new_window: + kwargs['newWindow'] = True + if background: + kwargs['background'] = True + if bid: + kwargs['browserContextId'] = bid - else: - tid = self.run_cdp('Target.createTarget', url='')['targetId'] - - return tid - - def new_tab(self, url=None, switch_to=False): - """新建一个标签页,该标签页在最后面 - :param url: 新标签页跳转到的网址 - :param switch_to: 新建标签页后是否把焦点移过去 - :return: switch_to为False时返回新标签页对象,否则返回当前对象, - """ - tid = self._new_tab(url, switch_to) - return self if switch_to else ChromiumTab(self, tid) - - def to_main_tab(self): - """跳转到主标签页""" - self.to_tab(self._main_tab) - - def to_tab(self, tab_or_id=None, activate=True): - """跳转到标签页 - :param tab_or_id: 标签页对象或id,默认跳转到main_tab - :param activate: 切换后是否变为活动状态 - :return: None - """ - self._to_tab(tab_or_id, activate) - - def _to_tab(self, tab_or_id=None, activate=True, read_doc=True): - """跳转到标签页 - :param tab_or_id: 标签页对象或id,默认跳转到main_tab - :param activate: 切换后是否变为活动状态 - :param read_doc: 切换后是否读取文档 - :return: None - """ - tabs = self.tabs - if not tab_or_id: - tab_id = self._main_tab - elif isinstance(tab_or_id, ChromiumTab): - tab_id = tab_or_id.tab_id - else: - tab_id = tab_or_id - - if tab_id not in tabs: - tab_id = self.latest_tab - - if activate: - self.browser.activate_tab(tab_id) - - if tab_id == self.tab_id: - return - - self.driver.stop() - self._driver_init(tab_id) - if read_doc and self.ready_state in ('complete', None): - self._get_document() + tid = self.run_cdp('Target.createTarget', **kwargs)['targetId'] + tab = ChromiumTab(self, tab_id=tid) + if url: + tab.get(url) + return tab def close_tabs(self, tabs_or_ids=None, others=False): """关闭传入的标签页,默认关闭当前页。可传入多个 @@ -250,20 +201,12 @@ class ChromiumPage(ChromiumBase): self.quit() return - if self.tab_id in tabs: - self.driver.stop() - for tab in tabs: self.browser.close_tab(tab) sleep(.2) while self.tabs_count != end_len: sleep(.1) - if self._main_tab in tabs: - self._main_tab = self.tabs[0] - - self.to_tab() - def close_other_tabs(self, tabs_or_ids=None): """关闭传入的标签页以外标签页,默认保留当前页。可传入多个 :param tabs_or_ids: 要保留的标签页对象或id,可传入列表或元组,为None时保存当前页 diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index a5f5ef7..9dc2102 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -21,7 +21,6 @@ class ChromiumPage(ChromiumBase): tab_id: str = None, timeout: float = None): self._driver_options: ChromiumOptions = ... - self._main_tab: str = ... self._browser: Browser = ... self._rect: Optional[TabRect] = ... @@ -63,20 +62,11 @@ class ChromiumPage(ChromiumBase): def find_tabs(self, title: str = None, url: str = None, tab_type: Union[str, list, tuple] = None, single: bool = True) -> Union[str, List[str]]: ... - def _new_tab(self, url=None, switch_to=False) -> str: ... + def new_tab(self, url: str = None, new_window: bool = False, background: bool = False, + new_context: bool = False) -> Union[ChromiumTab, ChromiumPage]: ... - def new_tab(self, url: str = None, switch_to: bool = False) -> Union[ChromiumTab, ChromiumPage]: ... - - def to_main_tab(self) -> None: ... - - def to_tab(self, tab_or_id: Union[str, ChromiumTab] = None, activate: bool = True) -> None: ... - - def _to_tab(self, tab_or_id: Union[str, ChromiumTab] = None, activate: bool = True, - read_doc: bool = True) -> None: ... - - def close_tabs(self, tabs_or_ids: Union[ - str, ChromiumTab, List[Union[str, ChromiumTab]], Tuple[Union[str, ChromiumTab]]] = None, - others: bool = False) -> None: ... + def close_tabs(self, tabs_or_ids: Union[str, ChromiumTab, List[Union[str, ChromiumTab]], + Tuple[Union[str, ChromiumTab]]] = None, others: bool = False) -> None: ... def close_other_tabs(self, tabs_or_ids: Union[ str, ChromiumTab, List[Union[str, ChromiumTab]], Tuple[Union[str, ChromiumTab]]] = None) -> None: ...