From b854df764c7496baa240be47a40d8e2173f8807c Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 14 Oct 2024 17:44:44 +0800 Subject: [PATCH] =?UTF-8?q?close=5Ftabs()=E7=9A=84tabs=5For=5Fids=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E6=94=B9=E4=B8=BA=E5=BF=85=E5=A1=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium.py | 11 +++-------- DrissionPage/_pages/chromium_page.py | 4 ++-- DrissionPage/_pages/chromium_page.pyi | 6 +++--- 3 files changed, 8 insertions(+), 13 deletions(-) diff --git a/DrissionPage/_base/chromium.py b/DrissionPage/_base/chromium.py index 082f764..20bb17d 100644 --- a/DrissionPage/_base/chromium.py +++ b/DrissionPage/_base/chromium.py @@ -191,27 +191,22 @@ class Chromium(object): def get_tabs(self, title=None, url=None, tab_type='page'): return self._get_tabs(title=title, url=url, tab_type=tab_type, mix=True, as_id=False) - def close_tabs(self, tabs_or_ids=None, others=False): - all_tabs = set(self.tab_ids) + def close_tabs(self, tabs_or_ids, others=False): if isinstance(tabs_or_ids, str): tabs = {tabs_or_ids} elif isinstance(tabs_or_ids, ChromiumTab): tabs = {tabs_or_ids.tab_id} - elif tabs_or_ids is None: - tabs = {self.tab_ids[0]} elif isinstance(tabs_or_ids, (list, tuple)): tabs = set(i.tab_id if isinstance(i, ChromiumTab) else i for i in tabs_or_ids) else: raise TypeError('tabs_or_ids参数只能传入标签页对象或id。') + all_tabs = set(self.tab_ids) if others: tabs = all_tabs - tabs - - end_len = len(set(all_tabs) - set(tabs)) - if end_len <= 0: + if len(all_tabs - tabs) <= 0: self.quit() return - for tab in tabs: self._close_tab(tab_id=tab) diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 360c98a..f8e6573 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -116,8 +116,8 @@ class ChromiumPage(ChromiumBase): def close(self): self.browser._close_tab(self.tab_id) - def close_tabs(self, tabs_or_ids=None, others=False): - self.browser.close_tabs(tabs_or_ids=tabs_or_ids or self.tab_id, others=others) + def close_tabs(self, tabs_or_ids, others=False): + self.browser.close_tabs(tabs_or_ids=tabs_or_ids, others=others) def quit(self, timeout=5, force=True, del_data=False): self.browser.quit(timeout, force, del_data=del_data) diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index 4d3b410..f90b167 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -198,10 +198,10 @@ class ChromiumPage(ChromiumBase): def close_tabs(self, tabs_or_ids: Union[str, ChromiumTab, List[Union[str, ChromiumTab]], - Tuple[Union[str, ChromiumTab]]] = None, + Tuple[Union[str, ChromiumTab]]], others: bool = False) -> None: - """关闭传入的标签页,默认关闭当前页。可传入多个 - :param tabs_or_ids: 要关闭的标签页对象或id,可传入列表或元组,为None时关闭当前页 + """关闭传入的标签页,可传入多个 + :param tabs_or_ids: 要关闭的标签页对象或id,可传入列表或元组 :param others: 是否关闭指定标签页之外的 :return: None """