From cd906c844001024d5185e2d6da1a8aa8e92f6693 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 20 Aug 2024 17:37:58 +0800 Subject: [PATCH] =?UTF-8?q?get=5Fmix=5Ftab()=E5=92=8Cget=5Fmix=5Ftabs()?= =?UTF-8?q?=E5=88=A0=E9=99=A4as=5Fid=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 16 ++++++++-------- DrissionPage/_base/browser.pyi | 6 ++---- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index a36d6be..3af6b07 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -163,7 +163,7 @@ class Chromium(object): @property def latest_tab(self): - return self.get_tab(self.tab_ids[0], as_id=not Settings.singleton_tab_obj) + return self._get_tab(id_or_num=self.tab_ids[0], as_id=not Settings.singleton_tab_obj) def cookies(self, all_info=False): cks = self._run_cdp(f'Storage.getCookies')['cookies'] @@ -187,14 +187,14 @@ class Chromium(object): def get_tabs(self, title=None, url=None, tab_type='page', as_id=False): return self._get_tabs(title=title, url=url, tab_type=tab_type, as_id=as_id) - def get_mix_tab(self, id_or_num=None, title=None, url=None, tab_type='page', as_id=False): - t = self._get_tab(id_or_num=id_or_num, title=title, url=url, tab_type=tab_type, mix=True, as_id=as_id) + def get_mix_tab(self, id_or_num=None, title=None, url=None, tab_type='page'): + t = self._get_tab(id_or_num=id_or_num, title=title, url=url, tab_type=tab_type, mix=True, as_id=False) if t._type != 'MixTab': raise RuntimeError('该标签页已有非MixTab版本,如需多对象公用请用Settings设置singleton_tab_obj为False。') return t - def get_mix_tabs(self, title=None, url=None, tab_type='page', as_id=False): - return self._get_tabs(title=title, url=url, tab_type=tab_type, mix=True, as_id=as_id) + def get_mix_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) @@ -328,12 +328,12 @@ class Chromium(object): def _get_tab(self, id_or_num=None, title=None, url=None, tab_type='page', mix=False, as_id=False): if id_or_num is not None: - if isinstance(id_or_num, str): - id_or_num = id_or_num - elif isinstance(id_or_num, int): + if isinstance(id_or_num, int): id_or_num = self.tab_ids[id_or_num - 1 if id_or_num > 0 else id_or_num] elif isinstance(id_or_num, ChromiumTab): return id_or_num.tab_id if as_id else ChromiumTab(self, id_or_num.tab_id) + elif id_or_num not in [i['id'] for i in self._driver.get(f'http://{self.address}/json').json()]: + raise ValueError(f'没有找到标签页{id_or_num},所有标签页:{self.tab_ids}') elif title == url is None and tab_type == 'page': id_or_num = self.tab_ids[0] diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index 1806849..5f1b499 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -194,8 +194,7 @@ class Chromium(object): id_or_num: Union[str, int] = None, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = 'page', - as_id: bool = False) -> Union[MixTab, str]: + tab_type: Union[str, list, tuple] = 'page') -> MixTab: """获取一个标签页对象,id_or_num不为None时,后面几个参数无效 :param id_or_num: 要获取的标签页id或序号,序号从1开始,可传入负数获取倒数第几个,不是视觉排列顺序,而是激活顺序,为None时获取最后的 :param title: 要匹配title的文本,模糊匹配,为None则匹配所有 @@ -209,8 +208,7 @@ class Chromium(object): def get_mix_tabs(self, title: str = None, url: str = None, - tab_type: Union[str, list, tuple] = 'page', - as_id: bool = False) -> List[MixTab, str]: + tab_type: Union[str, list, tuple] = 'page') -> List[MixTab]: """查找符合条件的tab,返回它们组成的列表,title和url是与关系 :param title: 要匹配title的文本 :param url: 要匹配url的文本