From 34c5ad818b1b0ce01e97e8977989aa29959c918c Mon Sep 17 00:00:00 2001 From: g1879 Date: Sat, 30 Dec 2023 14:40:20 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=83=E7=B4=A0=E5=AF=B9=E8=B1=A1=E9=97=B4?= =?UTF-8?q?=E5=8F=AF=E7=94=A8=3D=3D=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E6=8C=87=E5=90=91=E5=90=8C=E4=B8=80=E4=B8=AA=E5=85=83=E7=B4=A0?= =?UTF-8?q?=EF=BC=9BPage=E7=9A=84save()=E9=BB=98=E8=AE=A4=E8=BF=94?= =?UTF-8?q?=E5=9B=9E=E6=96=87=E6=9C=AC=E4=B8=8D=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 6 ++++++ DrissionPage/_elements/chromium_element.pyi | 4 ++++ DrissionPage/_elements/session_element.py | 3 +++ DrissionPage/_elements/session_element.pyi | 2 ++ DrissionPage/_pages/chromium_base.py | 8 +++++--- DrissionPage/_pages/chromium_frame.py | 3 +++ DrissionPage/_pages/chromium_frame.pyi | 2 ++ DrissionPage/_pages/chromium_page.py | 6 +++--- DrissionPage/_pages/chromium_tab.py | 6 +++--- 9 files changed, 31 insertions(+), 9 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 2a0a4f0..48d64e2 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -85,6 +85,9 @@ class ChromiumElement(DrissionElement): """ return self.ele(loc_or_str, timeout) + def __eq__(self, other): + return self._backend_id == getattr(other, '_backend_id', None) + @property def tag(self): """返回元素tag""" @@ -794,6 +797,9 @@ class ShadowRoot(BaseElement): """ return self.ele(loc_or_str, timeout) + def __eq__(self, other): + return self._backend_id == getattr(other, '_backend_id', None) + @property def tag(self): """返回元素标签名""" diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index 388a30c..4e3f886 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -48,6 +48,8 @@ class ChromiumElement(DrissionElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... + def __eq__(self, other: ChromiumElement) -> bool: ... + @property def tag(self) -> str: ... @@ -242,6 +244,8 @@ class ShadowRoot(BaseElement): def __call__(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None) -> ChromiumElement: ... + def __eq__(self, other: ShadowRoot) -> bool: ... + @property def states(self) -> ShadowRootStates: ... diff --git a/DrissionPage/_elements/session_element.py b/DrissionPage/_elements/session_element.py index 430e6c4..82820a9 100644 --- a/DrissionPage/_elements/session_element.py +++ b/DrissionPage/_elements/session_element.py @@ -43,6 +43,9 @@ class SessionElement(DrissionElement): """ return self.ele(loc_or_str) + def __eq__(self, other): + return self.xpath == getattr(other, 'xpath', None) + @property def tag(self): """返回元素类型""" diff --git a/DrissionPage/_elements/session_element.pyi b/DrissionPage/_elements/session_element.pyi index 190d06b..32452c6 100644 --- a/DrissionPage/_elements/session_element.pyi +++ b/DrissionPage/_elements/session_element.pyi @@ -30,6 +30,8 @@ class SessionElement(DrissionElement): loc_or_str: Union[Tuple[str, str], str], timeout: float = None) -> Union[SessionElement, NoneElement]: ... + def __eq__(self, other: SessionElement) -> bool: ... + @property def tag(self) -> str: ... diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 9ca83ed..df4476e 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -1153,13 +1153,15 @@ def close_privacy_dialog(page, tid): def get_mhtml(page, path=None, name=None): - """把当前页面保存为mhtml文件 + """把当前页面保存为mhtml文件,如果path和name参数都为None,只返回mhtml文本 :param page: 要保存的页面对象 - :param path: 保存路径,为None保存在当前路径 - :param name: 文件名,为None则用title属性值 + :param path: 保存路径,为None且name不为None时保存在当前路径 + :param name: 文件名,为None且path不为None时用title属性值 :return: mhtml文本 """ r = page.run_cdp('Page.captureSnapshot')['data'] + if path is None and name is None: + return r path = path or '.' Path(path).mkdir(parents=True, exist_ok=True) name = make_valid_name(name or page.title) diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 9abe951..80e0460 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -71,6 +71,9 @@ class ChromiumFrame(ChromiumBase): """ return self.ele(loc_or_str, timeout) + def __eq__(self, other): + return self._frame_id == getattr(other, '_frame_id', None) + def __repr__(self): attrs = self._frame_ele.attrs attrs = [f"{attr}='{attrs[attr]}'" for attr in attrs] diff --git a/DrissionPage/_pages/chromium_frame.pyi b/DrissionPage/_pages/chromium_frame.pyi index f2cf074..a2149bb 100644 --- a/DrissionPage/_pages/chromium_frame.pyi +++ b/DrissionPage/_pages/chromium_frame.pyi @@ -44,6 +44,8 @@ class ChromiumFrame(ChromiumBase): loc_or_str: Union[Tuple[str, str], str], timeout: float = None) -> Union[ChromiumElement, NoneElement]: ... + def __eq__(self, other: ChromiumFrame) -> bool: ... + def _check_alive(self) -> None: ... def __repr__(self) -> str: ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index 3f04953..2bfb04d 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -142,9 +142,9 @@ class ChromiumPage(ChromiumBase): return self.browser.process_id def save(self, path=None, name=None): - """把当前页面保存为mhtml文件 - :param path: 保存路径,为None保存在当前路径 - :param name: 文件名,为None则用title属性值 + """把当前页面保存为mhtml文件,如果path和name参数都为None,只返回mhtml文本 + :param path: 保存路径,为None且name不为None时保存在当前路径 + :param name: 文件名,为None且path不为None时用title属性值 :return: mhtml文本 """ return get_mhtml(self, path, name) diff --git a/DrissionPage/_pages/chromium_tab.py b/DrissionPage/_pages/chromium_tab.py index 124cc4d..06ae0fa 100644 --- a/DrissionPage/_pages/chromium_tab.py +++ b/DrissionPage/_pages/chromium_tab.py @@ -59,9 +59,9 @@ class ChromiumTab(ChromiumBase): return self._wait def save(self, path=None, name=None): - """把当前页面保存为mhtml文件 - :param path: 保存路径,为None保存在当前路径 - :param name: 文件名,为None则用title属性值 + """把当前页面保存为mhtml文件,如果path和name参数都为None,只返回mhtml文本 + :param path: 保存路径,为None且name不为None时保存在当前路径 + :param name: 文件名,为None且path不为None时用title属性值 :return: mhtml文本 """ return get_mhtml(self, path, name)