From bd4b91e915f9862f154c96fac782287ba92a22f5 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 28 Dec 2022 11:17:31 +0800 Subject: [PATCH] =?UTF-8?q?3.0.25=EF=BC=8C=E4=BF=AE=E5=A4=8D=E4=BD=8D?= =?UTF-8?q?=E7=BD=AE=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=EF=BC=9BSessionEl?= =?UTF-8?q?ement=E7=9A=84html=E5=B1=9E=E6=80=A7=E8=BF=94=E5=9B=9E=E5=B7=B2?= =?UTF-8?q?=E8=BD=AC=E7=A0=81=E6=96=87=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_element.py | 12 +----------- DrissionPage/chromium_element.pyi | 4 ++-- DrissionPage/chromium_frame.pyi | 4 ++++ DrissionPage/session_element.py | 15 ++++++++++----- DrissionPage/session_element.pyi | 7 ++++--- docs/版本历史.md | 2 +- setup.py | 2 +- 7 files changed, 23 insertions(+), 23 deletions(-) diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index c334c7f..83cbcb9 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -6,7 +6,6 @@ from os import sep from os.path import basename from pathlib import Path -from re import search from time import perf_counter, sleep from .base import DrissionElement, BaseElement @@ -75,20 +74,11 @@ class ChromiumElement(DrissionElement): @property def html(self): """返回元素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}' return self.page.run_cdp('DOM.getOuterHTML', nodeId=self._node_id, not_change=True)['outerHTML'] @property def inner_html(self): """返回元素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;') @property @@ -826,7 +816,7 @@ class ChromiumElement(DrissionElement): js = 'return document.documentElement.scrollLeft+" "+document.documentElement.scrollTop;' xy = self.run_script(js) 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): diff --git a/DrissionPage/chromium_element.pyi b/DrissionPage/chromium_element.pyi index aabc7ab..a432893 100644 --- a/DrissionPage/chromium_element.pyi +++ b/DrissionPage/chromium_element.pyi @@ -232,9 +232,9 @@ class ChromiumElement(DrissionElement): 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): diff --git a/DrissionPage/chromium_frame.pyi b/DrissionPage/chromium_frame.pyi index b8d0491..e35da16 100644 --- a/DrissionPage/chromium_frame.pyi +++ b/DrissionPage/chromium_frame.pyi @@ -33,6 +33,10 @@ class ChromiumFrame(ChromiumBase): def _get_new_document(self) -> None: ... + def _onFrameAttached(self, **kwargs): ... + + def _onFrameDetached(self, **kwargs): ... + @property def tab_id(self) -> str: ... diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index bcabae1..37a73ed 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -3,6 +3,7 @@ @Author : g1879 @Contact : g1879@qq.com """ +from html import unescape from re import match, DOTALL from lxml.etree import tostring @@ -49,7 +50,7 @@ class SessionElement(DrissionElement): def html(self): """返回outerHTML文本""" html = tostring(self._inner_ele, method="html").decode() - return html[:html.rfind('>') + 1] # tostring()会把跟紧元素的文本节点也带上,因此要去掉 + return unescape(html[:html.rfind('>') + 1]) # tostring()会把跟紧元素的文本节点也带上,因此要去掉 @property def inner_html(self): @@ -264,7 +265,8 @@ def make_session_ele(html_or_ele, loc=None, single=True): # ---------------根据传入对象类型获取页面对象和lxml元素对象--------------- the_type = str(type(html_or_ele)) - if the_type.endswith(".SessionElement'>"): # SessionElement + # SessionElement + if the_type.endswith(".SessionElement'>"): page = html_or_ele.page loc_str = loc[1] @@ -285,7 +287,8 @@ def make_session_ele(html_or_ele, loc=None, single=True): 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] if loc[0] == 'xpath' and loc[1].lstrip().startswith('/'): 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 = html_or_ele.xpath(xpath)[0] - elif isinstance(html_or_ele, BasePage): # 各种页面对象 + # 各种页面对象 + elif isinstance(html_or_ele, BasePage): page = html_or_ele html_or_ele = fromstring(html_or_ele.html) - elif isinstance(html_or_ele, str): # 直接传入html文本 + # 直接传入html文本 + elif isinstance(html_or_ele, str): page = None html_or_ele = fromstring(html_or_ele) diff --git a/DrissionPage/session_element.pyi b/DrissionPage/session_element.pyi index b3c1b07..61bd7e5 100644 --- a/DrissionPage/session_element.pyi +++ b/DrissionPage/session_element.pyi @@ -8,8 +8,9 @@ from typing import Union, List, Tuple from lxml.html import HtmlElement from .base import DrissionElement, BaseElement -from .chromium_base import ChromiumFrame, ChromiumBase +from .chromium_base import ChromiumBase from .chromium_element import ChromiumElement +from .chromium_frame import ChromiumFrame from .driver_element import DriverElement from .session_page import SessionPage @@ -111,7 +112,7 @@ class SessionElement(DrissionElement): def _get_ele_path(self, mode: str) -> str: ... -def make_session_ele(html_or_ele: Union[str, SessionElement, ChromiumElement, DriverElement, BaseElement, ChromiumFrame, - ChromiumBase], +def make_session_ele(html_or_ele: Union[str, SessionElement, SessionPage, ChromiumElement, DriverElement, BaseElement, + ChromiumFrame, ChromiumBase], loc: Union[str, Tuple[str, str]] = ..., single: bool = ...) -> Union[SessionElement, str, None, List[Union[SessionElement, str]]]: ... diff --git a/docs/版本历史.md b/docs/版本历史.md index e82810c..34e18c9 100644 --- a/docs/版本历史.md +++ b/docs/版本历史.md @@ -1,4 +1,4 @@ -# v3.0.23 +# v3.0.25 - 各种大小、位置信息从`dict`改为用`tuple`返回 diff --git a/setup.py b/setup.py index aca9951..f849b6d 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ with open("README.md", "r", encoding='utf-8') as fh: setup( name="DrissionPage", - version="3.0.23", + version="3.0.25", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",