3.0.25,修复位置返回类型;SessionElement的html属性返回已转码文本

This commit is contained in:
g1879 2022-12-28 11:17:31 +08:00
parent 4a812d2793
commit bd4b91e915
7 changed files with 23 additions and 23 deletions

View File

@ -6,7 +6,6 @@
from os import sep from os import sep
from os.path import basename from os.path import basename
from pathlib import Path from pathlib import Path
from re import search
from time import perf_counter, sleep from time import perf_counter, sleep
from .base import DrissionElement, BaseElement from .base import DrissionElement, BaseElement
@ -75,20 +74,11 @@ class ChromiumElement(DrissionElement):
@property @property
def html(self): def html(self):
"""返回元素outerHTML文本""" """返回元素outerHTML文本"""
tag = self.tag
if tag in ('iframe', 'frame'):
out_html = self.page.run_cdp('DOM.getOuterHTML', nodeId=self._node_id, not_change=True)['outerHTML']
in_html = self.inner_html
sign = search(rf'<{tag}.*?>', out_html).group(0)
return f'{sign}{in_html}</{tag}>'
return self.page.run_cdp('DOM.getOuterHTML', nodeId=self._node_id, not_change=True)['outerHTML'] return self.page.run_cdp('DOM.getOuterHTML', nodeId=self._node_id, not_change=True)['outerHTML']
@property @property
def inner_html(self): def inner_html(self):
"""返回元素innerHTML文本""" """返回元素innerHTML文本"""
if self.tag in ('iframe', 'frame'):
return self.run_script('return this.contentDocument.documentElement;').html
# return self.run_script(self, 'this.contentDocument.body;').html
return self.run_script('return this.innerHTML;') return self.run_script('return this.innerHTML;')
@property @property
@ -826,7 +816,7 @@ class ChromiumElement(DrissionElement):
js = 'return document.documentElement.scrollLeft+" "+document.documentElement.scrollTop;' js = 'return document.documentElement.scrollLeft+" "+document.documentElement.scrollTop;'
xy = self.run_script(js) xy = self.run_script(js)
sx, sy = xy.split(' ') sx, sy = xy.split(' ')
return {'x': x + int(float(sx)), 'y': y + int(float(sy))} return x + int(float(sx)), y + int(float(sy))
class ChromiumShadowRootElement(BaseElement): class ChromiumShadowRootElement(BaseElement):

View File

@ -232,9 +232,9 @@ class ChromiumElement(DrissionElement):
def _get_ele_path(self, mode: str) -> str: ... def _get_ele_path(self, mode: str) -> str: ...
def _get_client_rect(self, quad: str) -> Union[dict, None]: ... def _get_client_rect(self, quad: str) -> Union[list, None]: ...
def _get_absolute_rect(self, x: int, y: int) -> dict: ... def _get_absolute_rect(self, x: int, y: int) -> tuple: ...
class ChromiumShadowRootElement(BaseElement): class ChromiumShadowRootElement(BaseElement):

View File

@ -33,6 +33,10 @@ class ChromiumFrame(ChromiumBase):
def _get_new_document(self) -> None: ... def _get_new_document(self) -> None: ...
def _onFrameAttached(self, **kwargs): ...
def _onFrameDetached(self, **kwargs): ...
@property @property
def tab_id(self) -> str: ... def tab_id(self) -> str: ...

View File

@ -3,6 +3,7 @@
@Author : g1879 @Author : g1879
@Contact : g1879@qq.com @Contact : g1879@qq.com
""" """
from html import unescape
from re import match, DOTALL from re import match, DOTALL
from lxml.etree import tostring from lxml.etree import tostring
@ -49,7 +50,7 @@ class SessionElement(DrissionElement):
def html(self): def html(self):
"""返回outerHTML文本""" """返回outerHTML文本"""
html = tostring(self._inner_ele, method="html").decode() html = tostring(self._inner_ele, method="html").decode()
return html[:html.rfind('>') + 1] # tostring()会把跟紧元素的文本节点也带上,因此要去掉 return unescape(html[:html.rfind('>') + 1]) # tostring()会把跟紧元素的文本节点也带上,因此要去掉
@property @property
def inner_html(self): def inner_html(self):
@ -264,7 +265,8 @@ def make_session_ele(html_or_ele, loc=None, single=True):
# ---------------根据传入对象类型获取页面对象和lxml元素对象--------------- # ---------------根据传入对象类型获取页面对象和lxml元素对象---------------
the_type = str(type(html_or_ele)) the_type = str(type(html_or_ele))
if the_type.endswith(".SessionElement'>"): # SessionElement # SessionElement
if the_type.endswith(".SessionElement'>"):
page = html_or_ele.page page = html_or_ele.page
loc_str = loc[1] loc_str = loc[1]
@ -285,7 +287,8 @@ def make_session_ele(html_or_ele, loc=None, single=True):
loc = loc[0], loc_str loc = loc[0], loc_str
elif the_type.endswith((".ChromiumElement'>", ".DriverElement'>")): # ChromiumElement, DriverElement # ChromiumElement, DriverElement
elif the_type.endswith((".ChromiumElement'>", ".DriverElement'>")):
loc_str = loc[1] loc_str = loc[1]
if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'): if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'):
loc_str = f'.{loc[1]}' loc_str = f'.{loc[1]}'
@ -303,11 +306,13 @@ def make_session_ele(html_or_ele, loc=None, single=True):
html_or_ele = fromstring(html) html_or_ele = fromstring(html)
html_or_ele = html_or_ele.xpath(xpath)[0] html_or_ele = html_or_ele.xpath(xpath)[0]
elif isinstance(html_or_ele, BasePage): # 各种页面对象 # 各种页面对象
elif isinstance(html_or_ele, BasePage):
page = html_or_ele page = html_or_ele
html_or_ele = fromstring(html_or_ele.html) html_or_ele = fromstring(html_or_ele.html)
elif isinstance(html_or_ele, str): # 直接传入html文本 # 直接传入html文本
elif isinstance(html_or_ele, str):
page = None page = None
html_or_ele = fromstring(html_or_ele) html_or_ele = fromstring(html_or_ele)

View File

@ -8,8 +8,9 @@ from typing import Union, List, Tuple
from lxml.html import HtmlElement from lxml.html import HtmlElement
from .base import DrissionElement, BaseElement from .base import DrissionElement, BaseElement
from .chromium_base import ChromiumFrame, ChromiumBase from .chromium_base import ChromiumBase
from .chromium_element import ChromiumElement from .chromium_element import ChromiumElement
from .chromium_frame import ChromiumFrame
from .driver_element import DriverElement from .driver_element import DriverElement
from .session_page import SessionPage from .session_page import SessionPage
@ -111,7 +112,7 @@ class SessionElement(DrissionElement):
def _get_ele_path(self, mode: str) -> str: ... def _get_ele_path(self, mode: str) -> str: ...
def make_session_ele(html_or_ele: Union[str, SessionElement, ChromiumElement, DriverElement, BaseElement, ChromiumFrame, def make_session_ele(html_or_ele: Union[str, SessionElement, SessionPage, ChromiumElement, DriverElement, BaseElement,
ChromiumBase], ChromiumFrame, ChromiumBase],
loc: Union[str, Tuple[str, str]] = ..., loc: Union[str, Tuple[str, str]] = ...,
single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ...

View File

@ -1,4 +1,4 @@
# v3.0.23 # v3.0.25
- 各种大小、位置信息从`dict`改为用`tuple`返回 - 各种大小、位置信息从`dict`改为用`tuple`返回

View File

@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh:
setup( setup(
name="DrissionPage", name="DrissionPage",
version="3.0.23", version="3.0.25",
author="g1879", author="g1879",
author_email="g1879@qq.com", author_email="g1879@qq.com",
description="A module that integrates selenium and requests session, encapsulates common page operations.", description="A module that integrates selenium and requests session, encapsulates common page operations.",