From 6de716159fb210db701e34c542f915e7c86d0f64 Mon Sep 17 00:00:00 2001 From: g1879 Date: Wed, 3 Apr 2024 17:43:32 +0800 Subject: [PATCH] =?UTF-8?q?=E9=99=8D=E4=BD=8E=E5=A4=B1=E5=8E=BB=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E6=8A=A5=E9=94=99=E5=8F=AF=E8=83=BD=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_elements/chromium_element.py | 13 +++++++++++-- DrissionPage/_elements/chromium_element.pyi | 2 ++ DrissionPage/_functions/tools.pyi | 3 ++- DrissionPage/_pages/chromium_base.py | 4 ++-- DrissionPage/_units/rect.py | 3 ++- DrissionPage/_units/setter.py | 9 ++++++++- 6 files changed, 27 insertions(+), 7 deletions(-) diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index cfc58e7..b56263b 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -125,6 +125,10 @@ class ChromiumElement(DrissionElement): try: attrs = self.owner.run_cdp('DOM.getAttributes', nodeId=self._node_id)['attributes'] return {attrs[i]: attrs[i + 1] for i in range(0, len(attrs), 2)} + except ElementLostError: + self._refresh_id() + attrs = self.owner.run_cdp('DOM.getAttributes', nodeId=self._node_id)['attributes'] + return {attrs[i]: attrs[i + 1] for i in range(0, len(attrs), 2)} except CDPError: # 文档根元素不能调用此方法 return {} @@ -736,6 +740,11 @@ class ChromiumElement(DrissionElement): self._tag = n['localName'] return n['backendNodeId'] + def _refresh_id(self): + """根据backend id刷新其它id""" + self._obj_id = self._get_obj_id(backend_id=self._backend_id) + self._node_id = self._get_node_id(obj_id=self._obj_id) + def _get_ele_path(self, mode): """返获取绝对的css路径或xpath路径""" if mode == 'xpath': @@ -1106,8 +1115,8 @@ class ShadowRoot(BaseElement): r = make_chromium_eles(self.owner, _ids=node_id, is_obj_id=False) return None if r is False else r else: - node_ids = [self.owner.run_cdp('DOM.querySelector', nodeId=self._node_id, selector=i)['nodeId'] - for i in css] + node_ids = [self.owner.run_cdp('DOM.querySelector', + nodeId=self._node_id, selector=i)['nodeId'] for i in css] if 0 in node_ids: return None r = make_chromium_eles(self.owner, _ids=node_ids, index=index, is_obj_id=False) diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index 3e7d0da..e6a83a8 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -243,6 +243,8 @@ class ChromiumElement(DrissionElement): def _get_backend_id(self, node_id: int) -> int: ... + def _refresh_id(self) -> None: ... + def _get_ele_path(self, mode: str) -> str: ... diff --git a/DrissionPage/_functions/tools.pyi b/DrissionPage/_functions/tools.pyi index a6fc535..225ad81 100644 --- a/DrissionPage/_functions/tools.pyi +++ b/DrissionPage/_functions/tools.pyi @@ -10,6 +10,7 @@ from pathlib import Path from threading import Lock from typing import Union, Tuple +from ..errors import BaseError from .._pages.chromium_base import ChromiumBase @@ -45,4 +46,4 @@ def wait_until(function: callable, kwargs: dict = None, timeout: float = 10): .. def configs_to_here(file_name: Union[Path, str] = None) -> None: ... -def raise_error(result: dict, ignore=None) -> None: ... +def raise_error(result: dict, ignore: BaseError = None) -> None: ... diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 4920af4..027241e 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -30,7 +30,7 @@ from .._units.scroller import PageScroller from .._units.setter import ChromiumBaseSetter from .._units.states import PageStates from .._units.waiter import BaseWaiter -from ..errors import ContextLostError, CDPError, PageDisconnectedError, ElementNotFoundError +from ..errors import ContextLostError, CDPError, PageDisconnectedError, ElementNotFoundError, ElementLostError __ERROR__ = 'error' @@ -671,7 +671,7 @@ class ChromiumBase(BasePage): return ele = self._ele(loc_or_ele, raise_err=False) if ele: - self.run_cdp('DOM.removeNode', nodeId=ele._node_id) + self.run_cdp('DOM.removeNode', nodeId=ele._node_id, _ignore=ElementLostError) def add_ele(self, html_or_info, insert_to=None, before=None): """新建一个元素 diff --git a/DrissionPage/_units/rect.py b/DrissionPage/_units/rect.py index 3628f94..4428a16 100644 --- a/DrissionPage/_units/rect.py +++ b/DrissionPage/_units/rect.py @@ -102,7 +102,8 @@ class ElementRect(object): :return: 四个角坐标 """ return self._ele.owner.run_cdp('DOM.getBoxModel', backendNodeId=self._ele._backend_id, - nodeId=self._ele._node_id, objectId=self._ele._obj_id)['model'][quad] + # nodeId=self._ele._node_id, objectId=self._ele._obj_id + )['model'][quad] def _get_page_coord(self, x, y): """根据视口坐标获取绝对坐标""" diff --git a/DrissionPage/_units/setter.py b/DrissionPage/_units/setter.py index 9acbadd..8e576b2 100644 --- a/DrissionPage/_units/setter.py +++ b/DrissionPage/_units/setter.py @@ -14,6 +14,7 @@ from .cookies_setter import SessionCookiesSetter, CookiesSetter, WebPageCookiesS from .._functions.settings import Settings from .._functions.tools import show_or_hide_browser from .._functions.web import format_headers +from ..errors import ElementLostError class BasePageSetter(object): @@ -476,7 +477,13 @@ class ChromiumElementSetter(object): :param value: 属性值 :return: None """ - self._ele.owner.run_cdp('DOM.setAttributeValue', nodeId=self._ele._node_id, name=name, value=str(value)) + try: + self._ele.owner.run_cdp('DOM.setAttributeValue', + nodeId=self._ele._node_id, name=name, value=str(value)) + except ElementLostError: + self._ele._refresh_id() + self._ele.owner.run_cdp('DOM.setAttributeValue', + nodeId=self._ele._node_id, name=name, value=str(value)) def property(self, name, value): """设置元素property属性