From ba1c3fb3cceadb922616fbb7668e824e8aac7d60 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 17 Oct 2023 10:16:09 +0800 Subject: [PATCH] =?UTF-8?q?new=5Ftab()=E6=94=B9=E4=B8=BA=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=A1=B5=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_page.py | 13 ++++++++++--- DrissionPage/chromium_page.pyi | 6 ++++-- DrissionPage/web_page.py | 13 +++++++++---- DrissionPage/web_page.pyi | 6 +++--- 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 8e39bca..58215d3 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -172,8 +172,7 @@ class ChromiumPage(ChromiumBase): :param tab_id: 要获取的标签页id,为None时获取当前tab :return: 标签页对象 """ - tab_id = tab_id or self.tab_id - return ChromiumTab(self, tab_id) + return tab_id if isinstance(tab_id, ChromiumTab) else ChromiumTab(self, tab_id or self.tab_id) def find_tabs(self, title=None, url=None, tab_type=None, single=True): """查找符合条件的tab,返回它们的id组成的列表 @@ -197,7 +196,7 @@ class ChromiumPage(ChromiumBase): and (tab_type is None or i['type'] in tab_type))] return r[0]['id'] if r and single else r - def new_tab(self, url=None, switch_to=False): + def _new_tab(self, url=None, switch_to=False): """新建一个标签页,该标签页在最后面 :param url: 新标签页跳转到的网址 :param switch_to: 新建标签页后是否把焦点移过去 @@ -226,6 +225,14 @@ class ChromiumPage(ChromiumBase): return tid + def new_tab(self, url=None, switch_to=False): + """新建一个标签页,该标签页在最后面 + :param url: 新标签页跳转到的网址 + :param switch_to: 新建标签页后是否把焦点移过去 + :return: 新标签页对象 + """ + return ChromiumTab(self, self._new_tab(url, switch_to)) + def to_main_tab(self): """跳转到主标签页""" self.to_tab(self._main_tab) diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 9347094..497ea44 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -63,12 +63,14 @@ class ChromiumPage(ChromiumBase): @property def set(self) -> ChromiumPageSetter: ... - def get_tab(self, tab_id: str = None) -> ChromiumTab: ... + def get_tab(self, tab_id: Union[str, ChromiumTab] = None) -> ChromiumTab: ... def find_tabs(self, title: str = None, url: str = None, tab_type: Union[str, list, tuple, set] = None, single: bool = True) -> Union[str, List[str]]: ... - def new_tab(self, url: str = None, switch_to: bool = False) -> str: ... + def _new_tab(self, url: str = None, switch_to: bool = False) -> str: ... + + def new_tab(self, url: str = None, switch_to: bool = False) -> ChromiumTab: ... def to_main_tab(self) -> None: ... diff --git a/DrissionPage/web_page.py b/DrissionPage/web_page.py index b506c85..2d97b0c 100644 --- a/DrissionPage/web_page.py +++ b/DrissionPage/web_page.py @@ -374,8 +374,15 @@ class WebPage(SessionPage, ChromiumPage, BasePage): :param tab_id: 要获取的标签页id,为None时获取当前tab :return: 标签页对象 """ - tab_id = tab_id or self.tab_id - return WebPageTab(self, tab_id) + return tab_id if isinstance(tab_id, WebPageTab) else WebPageTab(self, tab_id or self.tab_id) + + def new_tab(self, url=None, switch_to=False): + """新建一个标签页,该标签页在最后面 + :param url: 新标签页跳转到的网址 + :param switch_to: 新建标签页后是否把焦点移过去 + :return: 新标签页对象 + """ + return WebPageTab(self, self._new_tab(url, switch_to)) def close_driver(self): """关闭driver及浏览器""" @@ -424,5 +431,3 @@ class WebPage(SessionPage, ChromiumPage, BasePage): super(SessionPage, self).quit() self._tab_obj = None self._has_driver = None - - diff --git a/DrissionPage/web_page.pyi b/DrissionPage/web_page.pyi index 1621fd9..5734631 100644 --- a/DrissionPage/web_page.pyi +++ b/DrissionPage/web_page.pyi @@ -124,7 +124,9 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def get_cookies(self, as_dict: bool = False, all_domains: bool = False, all_info: bool = False) -> Union[dict, list]: ... - def get_tab(self, tab_id: str = None) -> WebPageTab: ... + def get_tab(self, tab_id: Union[str, WebPageTab] = None) -> WebPageTab: ... + + def new_tab(self, url: str = None, switch_to: bool = False) -> WebPageTab: ... def close_driver(self) -> None: ... @@ -165,5 +167,3 @@ class WebPage(SessionPage, ChromiumPage, BasePage): def quit(self) -> None: ... def _on_download_begin(self, **kwargs): ... - -