From 7a509e511eb6153620945cfac54be01aa58b366a Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 20 Nov 2022 22:55:51 +0800 Subject: [PATCH] =?UTF-8?q?3.0.5=E3=80=82=E9=81=BF=E5=85=8D=E4=B8=8D?= =?UTF-8?q?=E5=90=8C=E7=BA=BF=E7=A8=8B=E9=87=8D=E5=A4=8D=E8=8E=B7=E5=8F=96?= =?UTF-8?q?document=EF=BC=9B=E4=BF=AE=E5=A4=8Dsave()=E4=B8=AD=E8=B0=83?= =?UTF-8?q?=E7=94=A8tab=5Fid=E5=B1=9E=E6=80=A7=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_element.py | 15 ++++++++++----- DrissionPage/chromium_page.py | 1 + DrissionPage/driver_element.py | 4 +++- DrissionPage/session_element.py | 4 +++- DrissionPage/shadow_root_element.py | 4 +++- setup.py | 2 +- 6 files changed, 21 insertions(+), 9 deletions(-) diff --git a/DrissionPage/chromium_element.py b/DrissionPage/chromium_element.py index 1f88bae..d365af3 100644 --- a/DrissionPage/chromium_element.py +++ b/DrissionPage/chromium_element.py @@ -517,7 +517,7 @@ class ChromiumElement(DrissionElement): node = self.page.driver.DOM.describeNode(nodeId=self._node_id)['node'] frame = node.get('frameId', None) - frame = frame or self.page.current_tab_id + frame = frame or self.page.tab_id result = self.page.driver.Page.getResourceContent(frameId=frame, url=src) if result['base64Encoded']: from base64 import b64decode @@ -1091,6 +1091,7 @@ class ChromiumBase(BasePage): self._control_session = Session() self._control_session.keep_alive = False self._first_run = True + self._is_reading = False # 用于避免不同线程重复读取document self.address = addr_tab_opts if not tab_id: @@ -1121,10 +1122,13 @@ class ChromiumBase(BasePage): def _get_document(self) -> None: """刷新cdp使用的document数据""" # print('get doc') - self._wait_loading() - root_id = self._tab_obj.DOM.getDocument()['root']['nodeId'] - self._root_id = self._tab_obj.DOM.resolveNode(nodeId=root_id)['object']['objectId'] - self._is_loading = False + if not self._is_reading: + self._is_reading = True + self._wait_loading() + root_id = self._tab_obj.DOM.getDocument()['root']['nodeId'] + self._root_id = self._tab_obj.DOM.resolveNode(nodeId=root_id)['object']['objectId'] + self._is_loading = False + self._is_reading = False def _wait_loading(self, timeout: float = None) -> bool: """等待页面加载完成 @@ -1778,6 +1782,7 @@ def make_chromium_ele(ele: ChromiumElement, :param loc: 元素定位元组 :param single: True则返回第一个,False则返回全部 :param timeout: 查找元素超时时间 + :param relative: WebPage用于标记是否相对定位使用 :return: 返回ChromiumElement元素或它们组成的列表 """ # ---------------处理定位符--------------- diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 592cb01..8ea2d5d 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -38,6 +38,7 @@ class ChromiumPage(ChromiumBase): :return: None """ self._is_loading = False + self._is_reading = False self._root_id = None self.timeouts = Timeout(self) self._control_session = Session() diff --git a/DrissionPage/driver_element.py b/DrissionPage/driver_element.py index 262bc2a..9d2c688 100644 --- a/DrissionPage/driver_element.py +++ b/DrissionPage/driver_element.py @@ -152,11 +152,13 @@ class DriverElement(DrissionElement): def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None, - single: bool = True) -> Union['DriverElement', str, None, List[Union['DriverElement', str]]]: + single: bool = True, + relative: bool = False) -> Union['DriverElement', str, None, List[Union['DriverElement', str]]]: """返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间 :param single: True则返回第一个,False则返回全部 + :param relative: WebPage用的表示是否相对定位的参数 :return: DriverElement对象 """ return make_driver_ele(self, loc_or_str, single, timeout) diff --git a/DrissionPage/session_element.py b/DrissionPage/session_element.py index 5cf9633..7412796 100644 --- a/DrissionPage/session_element.py +++ b/DrissionPage/session_element.py @@ -244,11 +244,13 @@ class SessionElement(DrissionElement): def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout=None, - single: bool = True) -> Union['SessionElement', str, None, List[Union['SessionElement', str]]]: + single: bool = True, + relative: bool = False) -> Union['SessionElement', str, None, List[Union['SessionElement', str]]]: """返回当前元素下级符合条件的子元素、属性或节点文本,默认返回第一个 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 不起实际作用,用于和父类对应 :param single: True则返回第一个,False则返回全部 + :param relative: WebPage用的表示是否相对定位的参数 :return: SessionElement对象 """ return make_session_ele(self, loc_or_str, single) diff --git a/DrissionPage/shadow_root_element.py b/DrissionPage/shadow_root_element.py index 3b87897..1301449 100644 --- a/DrissionPage/shadow_root_element.py +++ b/DrissionPage/shadow_root_element.py @@ -182,11 +182,13 @@ class ShadowRootElement(BaseElement): def _ele(self, loc_or_str: Union[Tuple[str, str], str], timeout: float = None, - single: bool = True) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: + single: bool = True, + relative: bool = False) -> Union[DriverElement, str, None, List[Union[DriverElement, str]]]: """返回当前元素下级符合条件的子元素,默认返回第一个 \n :param loc_or_str: 元素的定位信息,可以是loc元组,或查询字符串 :param timeout: 查找元素超时时间 :param single: True则返回第一个,False则返回全部 + :param relative: WebPage用的表示是否相对定位的参数 :return: DriverElement对象 """ # 先转换为sessionElement,再获取所有元素,获取它们的css selector路径,再用路径在页面上执行查找 diff --git a/setup.py b/setup.py index b31a23b..f6caf52 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.2", + version="3.0.5", author="g1879", author_email="g1879@qq.com", description="A module that integrates selenium and requests session, encapsulates common page operations.",