From 989a92adb79dee207a3ff37db5b33b7d2cd7b731 Mon Sep 17 00:00:00 2001 From: g1879 Date: Mon, 30 Oct 2023 18:09:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=BB=E5=8A=A8=E8=BF=9E=E6=8E=A5=E4=B9=9F?= =?UTF-8?q?=E9=81=B5=E5=BE=AA=E5=8A=A0=E8=BD=BD=E7=AD=96=E7=95=A5=E8=B6=85?= =?UTF-8?q?=E6=97=B6=EF=BC=9B=E4=BF=AE=E5=A4=8D=E4=B8=8B=E8=BD=BD=E8=B7=AF?= =?UTF-8?q?=E5=BE=84=E8=AE=BE=E7=BD=AE=E9=97=AE=E9=A2=98=EF=BC=9B=E5=A2=9E?= =?UTF-8?q?=E5=BC=BA=E8=AF=BB=E5=8F=96doc=E7=A8=B3=E5=AE=9A=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_pages/chromium_base.py | 28 ++++++++++++++++++++++--- DrissionPage/_pages/chromium_base.pyi | 2 +- DrissionPage/_pages/chromium_frame.py | 22 +++++++++++++------ DrissionPage/_units/download_manager.py | 2 +- MANIFEST.in | 2 +- setup.py | 2 +- 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/DrissionPage/_pages/chromium_base.py b/DrissionPage/_pages/chromium_base.py index bc36b5d..1adf588 100644 --- a/DrissionPage/_pages/chromium_base.py +++ b/DrissionPage/_pages/chromium_base.py @@ -7,6 +7,7 @@ from json import loads, JSONDecodeError from os.path import sep from pathlib import Path from re import findall +from threading import Thread from time import perf_counter, sleep from requests import get @@ -25,7 +26,7 @@ from .._units.screencast import Screencast from .._units.setter import ChromiumBaseSetter from .._units.waiter import ChromiumBaseWaiter from ..errors import (ContextLossError, ElementLossError, CDPError, TabClosedError, NoRectError, BrowserConnectError, - AlertExistsError) + AlertExistsError, GetDocumentError) class ChromiumBase(BasePage): @@ -126,8 +127,17 @@ class ChromiumBase(BasePage): if self._is_reading: return self._is_reading = True - b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId'] - self._root_id = self.run_cdp('DOM.resolveNode', backendNodeId=b_id)['object']['objectId'] + 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'] + break + except: + continue + else: + raise GetDocumentError + r = self.run_cdp('Page.getFrameTree') for i in findall(r"'id': '(.*?)'", str(r)): self.browser._frames[i] = self.tab_id @@ -172,6 +182,10 @@ class ChromiumBase(BasePage): if kwargs['frameId'] == self._frame_id: self._ready_state = 'loading' self._is_loading = True + if self.page_load_strategy == 'eager': + t = Thread(target=self._wait_to_stop) + t.daemon = True + t.start() if self._debug: print(f'frameStartedLoading {kwargs}') @@ -228,6 +242,14 @@ class ChromiumBase(BasePage): """ return self.ele(loc_or_str, timeout) + def _wait_to_stop(self): + """eager策略超时时使页面停止加载""" + end_time = perf_counter() + self.timeouts.page_load + while perf_counter() < end_time: + sleep(.1) + if self._ready_state in ('interactive', 'complete') and self._is_loading: + self.stop_loading() + @property def main(self): return self._page diff --git a/DrissionPage/_pages/chromium_base.pyi b/DrissionPage/_pages/chromium_base.pyi index 22812b3..7c34135 100644 --- a/DrissionPage/_pages/chromium_base.pyi +++ b/DrissionPage/_pages/chromium_base.pyi @@ -74,7 +74,7 @@ class ChromiumBase(BasePage): def _onFileChooserOpened(self, **kwargs): ... - # def _onDownloadWillBegin(self, **kwargs): ... + def _wait_to_stop(self): ... def _d_set_start_options(self, address, none) -> None: ... diff --git a/DrissionPage/_pages/chromium_frame.py b/DrissionPage/_pages/chromium_frame.py index 34214c0..fb8a047 100644 --- a/DrissionPage/_pages/chromium_frame.py +++ b/DrissionPage/_pages/chromium_frame.py @@ -149,14 +149,24 @@ class ChromiumFrame(ChromiumBase): if self._is_reading: return self._is_reading = True - if self._is_diff_domain is False: - node = self._target_page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node'] - self.doc_ele = ChromiumElement(self._target_page, - backend_id=node['contentDocument']['backendNodeId']) + end_time = perf_counter() + 10 + while perf_counter() < end_time: + try: + if self._is_diff_domain is False: + node = self._target_page.run_cdp('DOM.describeNode', backendNodeId=self.ids.backend_id)['node'] + self.doc_ele = ChromiumElement(self._target_page, + backend_id=node['contentDocument']['backendNodeId']) + + else: + b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId'] + self.doc_ele = ChromiumElement(self, backend_id=b_id) + + break + except: + continue else: - b_id = self.run_cdp('DOM.getDocument')['root']['backendNodeId'] - self.doc_ele = ChromiumElement(self, backend_id=b_id) + raise GetDocumentError r = self.run_cdp('Page.getFrameTree') for i in findall(r"'id': '(.*?)'", str(r)): diff --git a/DrissionPage/_units/download_manager.py b/DrissionPage/_units/download_manager.py index be55e9d..09b8afb 100644 --- a/DrissionPage/_units/download_manager.py +++ b/DrissionPage/_units/download_manager.py @@ -181,7 +181,7 @@ class BrowserDownloadManager(object): return mission.received_bytes = kwargs['receivedBytes'] mission.total_bytes = kwargs['totalBytes'] - form_path = f'{mission.save_path}{sep}{mission.id}' + form_path = f'{mission.path}{sep}{mission.id}' to_path = str(get_usable_path(f'{mission.path}{sep}{mission.name}')) move(form_path, to_path) self.set_done(mission, 'completed', final_path=to_path) diff --git a/MANIFEST.in b/MANIFEST.in index 4c619d1..962ba91 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,4 @@ -include DrissionPage/configs/configs.ini +include DrissionPage/_configs/configs.ini include DrissionPage/*.pyi include DrissionPage/*/*.py include DrissionPage/*/*.pyi \ No newline at end of file diff --git a/setup.py b/setup.py index 5007555..76574fa 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="4.0.0b1", + version="4.0.0b3", author="g1879", author_email="g1879@qq.com", description="Python based web automation tool. It can control the browser and send and receive data packets.",