From f13681e0df6af9de8669dbc398f5d58cd382507f Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 4 Jan 2024 00:28:46 +0800 Subject: [PATCH] =?UTF-8?q?ChromiumPage=E5=A2=9E=E5=8A=A0close()=EF=BC=8C?= =?UTF-8?q?=E5=88=A0=E9=99=A4close=5Fother=5Ftabs()=EF=BC=9B=E5=BE=AE?= =?UTF-8?q?=E8=B0=83quit()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 2 -- DrissionPage/_pages/chromium_page.py | 19 ++++++++++++------- DrissionPage/_pages/chromium_page.pyi | 5 ++--- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index ab17493..7d2e61f 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -169,9 +169,7 @@ class Browser(object): """ try: self.run_cdp('Browser.close') - self.driver.stop() except PageDisconnectedError: - self.driver.stop() return if force: diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index ff33dfd..c4a0a93 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -214,6 +214,10 @@ class ChromiumPage(ChromiumBase): return self.browser.run_cdp('Target.createTarget', **kwargs)['targetId'] + def close(self): + """关闭Page管理的标签页""" + self.browser.close_tab(self.tab_id) + def close_tabs(self, tabs_or_ids=None, others=False): """关闭传入的标签页,默认关闭当前页。可传入多个 :param tabs_or_ids: 要关闭的标签页对象或id,可传入列表或元组,为None时关闭当前页 @@ -247,13 +251,6 @@ class ChromiumPage(ChromiumBase): while self.tabs_count != end_len and perf_counter() < end_time: sleep(.1) - def close_other_tabs(self, tabs_or_ids=None): - """关闭传入的标签页以外标签页,默认保留当前页。可传入多个 - :param tabs_or_ids: 要保留的标签页对象或id,可传入列表或元组,为None时保存当前页 - :return: None - """ - self.close_tabs(tabs_or_ids, True) - def quit(self, timeout=5, force=True): """关闭浏览器 :param timeout: 等待浏览器关闭超时时间(秒) @@ -265,6 +262,14 @@ class ChromiumPage(ChromiumBase): def __repr__(self): return f'' + # ----------即将废弃----------- + def close_other_tabs(self, tabs_or_ids=None): + """关闭传入的标签页以外标签页,默认保留当前页。可传入多个 + :param tabs_or_ids: 要保留的标签页对象或id,可传入列表或元组,为None时保存当前页 + :return: None + """ + self.close_tabs(tabs_or_ids, True) + def get_rename(original, rename): if '.' in rename: diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index ac29e10..6617fde 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -67,12 +67,11 @@ class ChromiumPage(ChromiumBase): def _new_tab(self, new_window: bool = False, background: bool = False, new_context: bool = False) -> str: ... + def close(self) -> 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: ... - def quit(self, timeout: float = 5, force: bool = True) -> None: ...