From ddd7aba9ae62d961fe73b8d240cd39cd984228f7 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 27 Nov 2023 22:08:38 +0800 Subject: [PATCH] =?UTF-8?q?get=5Ftab()=E5=8F=82=E6=95=B0=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=8C=E5=8F=AF=E6=8E=A5=E6=94=B6=E5=BA=8F=E5=8F=B7=EF=BC=9B?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D8x=E7=89=88=E6=B5=8F=E8=A7=88=E5=99=A8select?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_page.py | 15 ++++++++++++--- DrissionPage/_pages/chromium_page.pyi | 2 +- DrissionPage/_pages/web_page.py | 15 ++++++++++++--- DrissionPage/_pages/web_page.pyi | 2 +- DrissionPage/_units/select_element.py | 2 +- 5 files changed, 27 insertions(+), 9 deletions(-) diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 677ce88..5c825be 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -138,12 +138,21 @@ class ChromiumPage(ChromiumBase): """返回浏览器进程id""" return self.browser.process_id - def get_tab(self, tab_id=None): + def get_tab(self, id_or_num=None): """获取一个标签页对象 - :param tab_id: 要获取的标签页id,为None时获取当前tab + :param id_or_num: 要获取的标签页id或序号,为None时获取当前tab,序号不是视觉排列顺序,而是激活顺序 :return: 标签页对象 """ - return tab_id if isinstance(tab_id, ChromiumTab) else ChromiumTab(self, tab_id or self.tab_id) + if isinstance(id_or_num, str): + return ChromiumTab(self, id_or_num) + elif isinstance(id_or_num, int): + return ChromiumTab(self, self.tabs[id_or_num]) + elif id_or_num is None: + return ChromiumTab(self, self.tab_id) + elif isinstance(id_or_num, ChromiumTab): + return id_or_num + else: + raise TypeError(f'id_or_num需传入tab id或序号,非{id_or_num}。') def find_tabs(self, title=None, url=None, tab_type=None, single=True): """查找符合条件的tab,返回它们的id组成的列表 diff --git a/DrissionPage/_pages/chromium_page.pyi b/DrissionPage/_pages/chromium_page.pyi index b4bc381..49e519b 100644 --- a/DrissionPage/_pages/chromium_page.pyi +++ b/DrissionPage/_pages/chromium_page.pyi @@ -54,7 +54,7 @@ class ChromiumPage(ChromiumBase): @property def set(self) -> ChromiumPageSetter: ... - def get_tab(self, tab_id: Union[str, ChromiumTab] = None) -> ChromiumTab: ... + def get_tab(self, tab_id: Union[str, ChromiumTab, int] = None) -> ChromiumTab: ... def find_tabs(self, title: str = None, url: str = None, tab_type: Union[str, list, tuple] = None, single: bool = True) -> Union[str, List[str]]: ... diff --git a/DrissionPage/_pages/web_page.py b/DrissionPage/_pages/web_page.py index 5e7db55..339436d 100644 --- a/DrissionPage/_pages/web_page.py +++ b/DrissionPage/_pages/web_page.py @@ -297,12 +297,21 @@ class WebPage(SessionPage, ChromiumPage, BasePage): elif self._mode == 'd': return super(SessionPage, self).get_cookies(as_dict, all_domains, all_info) - def get_tab(self, tab_id=None): + def get_tab(self, id_or_num=None): """获取一个标签页对象 - :param tab_id: 要获取的标签页id,为None时获取当前tab + :param id_or_num: 要获取的标签页id或序号,为None时获取当前tab,序号不是视觉排列顺序,而是激活顺序 :return: 标签页对象 """ - return tab_id if isinstance(tab_id, WebPageTab) else WebPageTab(self, tab_id or self.tab_id) + if isinstance(id_or_num, str): + return WebPageTab(self, id_or_num) + elif isinstance(id_or_num, int): + return WebPageTab(self, self.tabs[id_or_num]) + elif id_or_num is None: + return WebPageTab(self, self.tab_id) + elif isinstance(id_or_num, WebPageTab): + return id_or_num + else: + raise TypeError(f'id_or_num需传入tab id或序号,非{id_or_num}。') def new_tab(self, url=None, new_window=False, background=False, new_context=False): """新建一个标签页 diff --git a/DrissionPage/_pages/web_page.pyi b/DrissionPage/_pages/web_page.pyi index bdbd81f..7931c75 100644 --- a/DrissionPage/_pages/web_page.pyi +++ b/DrissionPage/_pages/web_page.pyi @@ -122,7 +122,7 @@ 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: Union[str, WebPageTab] = None) -> WebPageTab: ... + def get_tab(self, id_or_num: Union[str, WebPageTab, int] = None) -> WebPageTab: ... def new_tab(self, url: str = None, new_window: bool = False, background: bool = False, new_context: bool = False) -> WebPageTab: ... diff --git a/DrissionPage/_units/select_element.py b/DrissionPage/_units/select_element.py index 9399bb4..9494d16 100644 --- a/DrissionPage/_units/select_element.py +++ b/DrissionPage/_units/select_element.py @@ -36,7 +36,7 @@ class SelectElement(object): @property def options(self): """返回所有选项元素组成的列表""" - return self._ele.eles('xpath://option') + return [i for i in self._ele.eles('xpath://option') if not isinstance(i, int)] @property def selected_option(self):