元素对象间可用==判断是否指向同一个元素;Page的save()默认返回文本不保存

This commit is contained in:
g1879 2023-12-30 14:40:20 +08:00
parent 6f5020f955
commit 34c5ad818b
9 changed files with 31 additions and 9 deletions

View File

@ -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):
"""返回元素标签名"""

View File

@ -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: ...

View File

@ -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):
"""返回元素类型"""

View File

@ -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: ...

View File

@ -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)

View File

@ -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]

View File

@ -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: ...

View File

@ -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)

View File

@ -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)