From a5b4951b5b2884cd6c8310e7ebeaca3021021060 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 26 Feb 2024 22:48:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E9=A1=B5=E9=9D=A2=E7=94=A8cs?= =?UTF-8?q?s=E6=9F=A5=E6=89=BE=E5=85=83=E7=B4=A0=E6=97=B6=E4=BC=9A?= =?UTF-8?q?=E6=89=BE=E5=88=B0=E6=96=87=E6=9C=AC=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/base.py | 2 +- DrissionPage/_elements/chromium_element.py | 32 +++++++++++++++------ DrissionPage/_elements/chromium_element.pyi | 3 +- DrissionPage/_pages/chromium_base.py | 3 +- DrissionPage/_units/listener.py | 4 ++- 5 files changed, 31 insertions(+), 13 deletions(-) diff --git a/DrissionPage/_base/base.py b/DrissionPage/_base/base.py index d67ccb6..205baaa 100644 --- a/DrissionPage/_base/base.py +++ b/DrissionPage/_base/base.py @@ -54,7 +54,7 @@ class BaseElement(BaseParser): def __init__(self, owner=None): self.owner = owner - self.page = owner._page + self.page = owner._page if owner else None self._type = 'BaseElement' # ----------------以下属性或方法由后代实现---------------- diff --git a/DrissionPage/_elements/chromium_element.py b/DrissionPage/_elements/chromium_element.py index 4d4e872..8cb416b 100644 --- a/DrissionPage/_elements/chromium_element.py +++ b/DrissionPage/_elements/chromium_element.py @@ -1271,12 +1271,13 @@ def find_by_css(ele, selector, index, timeout): return NoneElement(ele.owner) if index is not None else [] -def make_chromium_eles(page, _ids, index=1, is_obj_id=True): +def make_chromium_eles(page, _ids, index=1, is_obj_id=True, ele_only=False): """根据node id或object id生成相应元素对象 :param page: ChromiumPage对象 :param _ids: 元素的id列表 :param index: 获取第几个,为None返回全部 :param is_obj_id: 传入的id是obj id还是node id + :param ele_only: 是否只返回ele,在页面查找元素时生效 :return: 浏览器元素对象或它们组成的列表,生成失败返回False """ if is_obj_id: @@ -1287,16 +1288,27 @@ def make_chromium_eles(page, _ids, index=1, is_obj_id=True): _ids = (_ids,) if index is not None: # 获取一个 - obj_id = _ids[index - 1] - return get_node_func(page, obj_id) + if ele_only: + for obj_id in _ids: + tmp = get_node_func(page, obj_id, ele_only) + if tmp is False: + return False + elif tmp is not None: + return tmp + return False + + else: + obj_id = _ids[index - 1] + return get_node_func(page, obj_id, ele_only) else: # 获取全部 nodes = [] for obj_id in _ids: - tmp = get_node_func(page, obj_id) + tmp = get_node_func(page, obj_id, ele_only) if tmp is False: return False - nodes.append(tmp) + elif tmp is not None: + nodes.append(tmp) return nodes @@ -1310,22 +1322,24 @@ def _get_node_info(page, id_type, _id): return node -def _get_node_by_obj_id(page, obj_id): +def _get_node_by_obj_id(page, obj_id, ele_only): + """根据obj id返回元素对象或文本,ele_only时如果是文本返回None,出错返回False""" node = _get_node_info(page, 'objectId', obj_id) if node is False: return False if node['node']['nodeName'] in ('#text', '#comment'): - return node['node']['nodeValue'] + return None if ele_only else node['node']['nodeValue'] else: return _make_ele(page, obj_id, node) -def _get_node_by_node_id(page, node_id): +def _get_node_by_node_id(page, node_id, ele_only): + """根据node id返回元素对象或文本,ele_only时如果是文本返回None,出错返回False""" node = _get_node_info(page, 'nodeId', node_id) if node is False: return False if node['node']['nodeName'] in ('#text', '#comment'): - return node['node']['nodeValue'] + return None if ele_only else node['node']['nodeValue'] else: obj_id = page.driver.run('DOM.resolveNode', nodeId=node_id) if 'error' in obj_id: diff --git a/DrissionPage/_elements/chromium_element.pyi b/DrissionPage/_elements/chromium_element.pyi index b1defa4..730ebb1 100644 --- a/DrissionPage/_elements/chromium_element.pyi +++ b/DrissionPage/_elements/chromium_element.pyi @@ -356,7 +356,8 @@ def find_by_css(ele: ChromiumElement, def make_chromium_eles(page: Union[ChromiumBase, ChromiumPage, WebPage, ChromiumTab, ChromiumFrame], _ids: Union[tuple, list, str, int], index: Optional[int] = 1, - is_obj_id: bool = True + is_obj_id: bool = True, + ele_only: bool = False ) -> Union[ChromiumElement, ChromiumFrame, List[Union[ChromiumElement, ChromiumFrame]]]: ... diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index 1851972..7ee9123 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -576,7 +576,8 @@ class ChromiumBase(BasePage): fromIndex=from_index, toIndex=end_index) if __ERROR__ not in nIds: if nIds['nodeIds'][0] != 0: - r = make_chromium_eles(self, _ids=nIds['nodeIds'], index=index_arg, is_obj_id=False) + r = make_chromium_eles(self, _ids=nIds['nodeIds'], index=index_arg, + is_obj_id=False, ele_only=True) if r is not False: break diff --git a/DrissionPage/_units/listener.py b/DrissionPage/_units/listener.py index 80ebe03..dd5de19 100644 --- a/DrissionPage/_units/listener.py +++ b/DrissionPage/_units/listener.py @@ -162,6 +162,8 @@ class Listener(object): :param gap: 每接收到多少个数据包返回一次数据 :return: 用于在接收到监听目标时触发动作的可迭代对象 """ + if not self.listening: + raise RuntimeError('监听未启动或已暂停。') caught = 0 end = perf_counter() + timeout if timeout else None while True: @@ -221,7 +223,7 @@ class Listener(object): :return: 返回是否等待成功 """ if not self.listening: - raise RuntimeError('监听未启动,用listen.start()启动。') + raise RuntimeError('监听未启动或已暂停。') if timeout is None: while (not targets_only and self._running_requests > 0) or (targets_only and self._running_targets > 0): sleep(.1)