From cb66d974a38f1fc07a4befa945fbc55dcdc71bc6 Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 21 Feb 2023 17:40:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E8=8E=B7=E5=8F=96=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E7=9E=AC=E9=97=B4=E5=85=83=E7=B4=A0=E8=A2=AB=E5=88=B7?= =?UTF-8?q?=E6=96=B0=E5=A4=84=E7=90=86=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 8496de2..559e1fb 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -338,7 +338,8 @@ class ChromiumBase(BasePage): error = r[ERROR] if error == 'Cannot find context with specified id': raise ContextLossError - elif error.startswith('Could not find ') and error.endswith(' id'): + elif error in ('Could not find node with given id', 'Could not find object with given id', + 'No node with given id found'): raise ElementLossError elif error == 'tab closed': raise TabClosedError @@ -465,11 +466,13 @@ class ChromiumBase(BasePage): ok = False nodeIds = None - search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True) - count = search_result['resultCount'] timeout = timeout if timeout is not None else self.timeout end_time = perf_counter() + timeout + + search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True) + count = search_result['resultCount'] + while True: if count > 0: count = 1 if single else count @@ -482,19 +485,21 @@ class ChromiumBase(BasePage): except Exception: sleep(.01) - if ok or perf_counter() >= end_time: - break + if ok: + try: + if single: + return make_chromium_ele(self, node_id=nodeIds['nodeIds'][0]) + else: + return [make_chromium_ele(self, node_id=i) for i in nodeIds['nodeIds']] + + except ElementLossError: + ok = False search_result = self.run_cdp_loaded('DOM.performSearch', query=loc, includeUserAgentShadowDOM=True) count = search_result['resultCount'] - if not nodeIds: - return NoneElement() if single else [] - - if single: - return make_chromium_ele(self, node_id=nodeIds['nodeIds'][0]) - else: - return [make_chromium_ele(self, node_id=i) for i in nodeIds['nodeIds']] + if perf_counter() >= end_time: + return NoneElement() if single else [] def refresh(self, ignore_cache=False): """刷新当前页面