From a704c12f3837ddbadb61a21cd25e0274c4874c7a Mon Sep 17 00:00:00 2001 From: g1879 Date: Fri, 31 Mar 2023 18:26:33 +0800 Subject: [PATCH] =?UTF-8?q?handle=5Falert()=E7=AD=89=E5=BE=85=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E6=97=B6=E8=BF=94=E5=9B=9EFalse=EF=BC=9Bwhile?= =?UTF-8?q?=E5=9D=87=E8=AE=BE=E7=BD=AE=E8=B6=85=E6=97=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_base.py | 18 +++++++++++++----- DrissionPage/chromium_frame.py | 24 ++++++++++++++++++------ DrissionPage/chromium_page.py | 6 +++--- DrissionPage/chromium_page.pyi | 2 +- 4 files changed, 35 insertions(+), 15 deletions(-) diff --git a/DrissionPage/chromium_base.py b/DrissionPage/chromium_base.py index 04b44d3..ebbcb3a 100644 --- a/DrissionPage/chromium_base.py +++ b/DrissionPage/chromium_base.py @@ -124,7 +124,8 @@ class ChromiumBase(BasePage): except TabClosedError: return - while True: + end_time = perf_counter() + 10 + while perf_counter() < end_time: try: b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId'] self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId'] @@ -133,8 +134,15 @@ class ChromiumBase(BasePage): break except Exception: - if self._debug_recorder: - self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错')) + if self._debug: + print('重试获取document') + if self._debug_recorder: + self._debug_recorder.add_data((perf_counter(), 'err', '读取root_id出错')) + + sleep(.1) + + else: + raise RuntimeError('获取document失败。') if self._debug: print('获取document结束') @@ -606,7 +614,7 @@ class ChromiumBase(BasePage): self._debug_recorder.add_data((perf_counter(), '操作', '停止页面加载')) self.run_cdp('Page.stopLoading') - while self.ready_state != 'complete': + while self.ready_state not in ('complete', None): sleep(.1) def remove_ele(self, loc_or_ele): @@ -770,7 +778,7 @@ class ChromiumBase(BasePage): if t < times: sleep(interval) - while self.ready_state != 'complete': + while self.ready_state not in ('complete', None): sleep(.1) if self._debug or show_errmsg: print(f'重试 {to_url}') diff --git a/DrissionPage/chromium_frame.py b/DrissionPage/chromium_frame.py index 09cacb7..fe4d308 100644 --- a/DrissionPage/chromium_frame.py +++ b/DrissionPage/chromium_frame.py @@ -109,7 +109,8 @@ class ChromiumFrame(ChromiumBase): if self._debug: print('---获取document') - while True: + end_time = perf_counter() + 10 + while perf_counter() < end_time: try: if self._is_diff_domain is False: node = self.page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node'] @@ -124,6 +125,9 @@ class ChromiumFrame(ChromiumBase): except Exception: sleep(.1) + else: + raise RuntimeError('获取document失败。') + if self._debug: print('---获取document结束') @@ -272,13 +276,21 @@ class ChromiumFrame(ChromiumBase): return 'complete' else: - while True: + end_time = perf_counter() + 10 + while perf_counter() < end_time: try: return self.doc_ele.run_js('return this.readyState;') except ContextLossError: - node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node'] - doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId']) - return doc.run_js('return this.readyState;') + try: + node = self.run_cdp('DOM.describeNode', backendNodeId=self.frame_ele.ids.backend_id)['node'] + doc = ChromiumElement(self.page, backend_id=node['contentDocument']['backendNodeId']) + return doc.run_js('return this.readyState;') + except: + pass + + sleep(.1) + + raise RuntimeError('获取document失败。') @property def scroll(self): @@ -558,7 +570,7 @@ class ChromiumFrame(ChromiumBase): if t < times: sleep(interval) - while self.ready_state != 'complete': + while self.ready_state not in ('complete', None): sleep(.1) if self._debug: print('重试') diff --git a/DrissionPage/chromium_page.py b/DrissionPage/chromium_page.py index 8d7a632..a0b3bad 100644 --- a/DrissionPage/chromium_page.py +++ b/DrissionPage/chromium_page.py @@ -261,7 +261,7 @@ class ChromiumPage(ChromiumBase): self.driver.stop() self._driver_init(tab_id) - if read_doc and self.ready_state == 'complete': + if read_doc and self.ready_state in ('complete', None): self._get_document() def close_tabs(self, tabs_or_ids=None, others=False): @@ -315,7 +315,7 @@ class ChromiumPage(ChromiumBase): :param accept: True表示确认,False表示取消,其它值不会按按钮但依然返回文本值 :param send: 处理prompt提示框时可输入文本 :param timeout: 等待提示框出现的超时时间,为None则使用self.timeout属性的值 - :return: 提示框内容文本,未等到提示框则返回None + :return: 提示框内容文本,未等到提示框则返回False """ timeout = self.timeout if timeout is None else timeout timeout = .1 if timeout <= 0 else timeout @@ -323,7 +323,7 @@ class ChromiumPage(ChromiumBase): while not self._alert.activated and perf_counter() < end_time: sleep(.1) if not self._alert.activated: - return None + return False res_text = self._alert.text if self._alert.type == 'prompt': diff --git a/DrissionPage/chromium_page.pyi b/DrissionPage/chromium_page.pyi index 4d63200..4b9e4cd 100644 --- a/DrissionPage/chromium_page.pyi +++ b/DrissionPage/chromium_page.pyi @@ -97,7 +97,7 @@ class ChromiumPage(ChromiumBase): def close_other_tabs(self, tabs_or_ids: Union[ str, ChromiumTab, List[Union[str, ChromiumTab]], Tuple[Union[str, ChromiumTab]]] = None) -> None: ... - def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, None]: ... + def handle_alert(self, accept: bool = True, send: str = None, timeout: float = None) -> Union[str, False]: ... def quit(self) -> None: ...