From 9d3b7f3ec1fa753edcd1c20cc7a64e129221e98b Mon Sep 17 00:00:00 2001 From: g1879 Date: Thu, 24 Oct 2024 18:14:11 +0800 Subject: [PATCH] =?UTF-8?q?click.to=5Fdownload()=E5=88=A0=E9=99=A4new=5Fta?= =?UTF-8?q?b=E5=8F=82=E6=95=B0=EF=BC=9B=E4=BC=98=E5=8C=96=E4=B8=8B?= =?UTF-8?q?=E8=BD=BD=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/chromium.py | 4 ++++ DrissionPage/_base/chromium.pyi | 1 + DrissionPage/_pages/chromium_page.py | 1 + DrissionPage/_units/clicker.py | 29 +++++----------------------- DrissionPage/_units/clicker.pyi | 2 -- DrissionPage/_units/downloader.py | 18 ++++++++++++++--- DrissionPage/_units/downloader.pyi | 4 +++- 7 files changed, 29 insertions(+), 30 deletions(-) diff --git a/DrissionPage/_base/chromium.py b/DrissionPage/_base/chromium.py index b14606e..5f51576 100644 --- a/DrissionPage/_base/chromium.py +++ b/DrissionPage/_base/chromium.py @@ -64,6 +64,7 @@ class Chromium(object): self._frames = {} self._drivers = {} self._all_drivers = {} + self._relation = {} self._newest_tab_id = None self._set = None @@ -388,7 +389,9 @@ class Chromium(object): and not kwargs['targetInfo']['url'].startswith('devtools://')): try: tab_id = kwargs['targetInfo']['targetId'] + self._frames[tab_id] = tab_id d = Driver(tab_id, 'page', self.address) + self._relation[tab_id] = kwargs['targetInfo'].get('openerId', None) self._drivers[tab_id] = d self._all_drivers.setdefault(tab_id, set()).add(d) self._newest_tab_id = tab_id @@ -404,6 +407,7 @@ class Chromium(object): d.stop() self._drivers.pop(tab_id, None) self._all_drivers.pop(tab_id, None) + self._relation.pop(tab_id, None) def _on_disconnect(self): if not self._disconnect_flag: diff --git a/DrissionPage/_base/chromium.pyi b/DrissionPage/_base/chromium.pyi index 778dc4d..1405dd2 100644 --- a/DrissionPage/_base/chromium.pyi +++ b/DrissionPage/_base/chromium.pyi @@ -40,6 +40,7 @@ class Chromium(object): _frames: dict = ... _drivers: Dict[str, Driver] = ... _all_drivers: Dict[str, Set[Driver]] = ... + _relation: Dict[str, Optional[str]] = ... _process_id: Optional[int] = ... _dl_mgr: DownloadManager = ... _timeouts: Timeout = ... diff --git a/DrissionPage/_pages/chromium_page.py b/DrissionPage/_pages/chromium_page.py index c5941de..76a2eee 100644 --- a/DrissionPage/_pages/chromium_page.py +++ b/DrissionPage/_pages/chromium_page.py @@ -43,6 +43,7 @@ class ChromiumPage(ChromiumBase): self._type = 'ChromiumPage' self.set.timeouts(base=timeout) # 即将废弃 self._tab = self + self._browser._dl_mgr._page_id = self.tab_id def __repr__(self): return f'' diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py index 4a31f54..2ce5ed2 100644 --- a/DrissionPage/_units/clicker.py +++ b/DrissionPage/_units/clicker.py @@ -10,7 +10,6 @@ from time import perf_counter, sleep from .waiter import wait_mission from .._functions.settings import Settings from .._functions.web import offset_scroll -from .._units.downloader import TabDownloadSettings from ..errors import CanNotClickError, CDPError, NoRectError, AlertExistsError @@ -119,35 +118,20 @@ class Clicker(object): def multi(self, times=2): return self.at(count=times) - def to_download(self, save_path=None, rename=None, suffix=None, new_tab=False, by_js=False, timeout=None): + def to_download(self, save_path=None, rename=None, suffix=None, by_js=False, timeout=None, new_tab=None): + # 即将废弃new_tab参数 if not self._ele.tab._browser._dl_mgr._running: self._ele.tab._browser.set.download_path('.') - when_file_exists = None tmp_path = None if self._ele.tab._type.endswith('Page'): obj = browser = self._ele.owner._browser tid = 'browser' - elif new_tab: - obj = browser = self._ele.owner._browser - tid = 'browser' - t_settings = TabDownloadSettings(self._ele.owner.tab_id) - b_settings = TabDownloadSettings('browser') - - when_file_exists = b_settings.when_file_exists - b_settings.when_file_exists = t_settings.when_file_exists - b_settings.rename = t_settings.rename - b_settings.suffix = t_settings.suffix - t_settings.rename = None - t_settings.suffix = None - if not save_path: - tmp_path = b_settings.path - b_settings.path = t_settings.path - else: obj = self._ele.owner._tab browser = obj.browser + browser._dl_mgr._waiting_tab.add(self._ele.owner.tab_id) tid = obj.tab_id if save_path: @@ -164,11 +148,8 @@ class Clicker(object): if tmp_path: obj.set.download_path(tmp_path) - if when_file_exists: - browser.set.when_download_file_exists(when_file_exists) - if m and new_tab: - self._ele.owner.browser._dl_mgr._tab_missions.setdefault(self._ele.owner.tab_id, []).append(m) - m.from_tab = self._ele.owner.tab_id + browser._dl_mgr._waiting_tab.discard(self._ele.owner.tab_id) + return m def to_upload(self, file_paths, by_js=False): diff --git a/DrissionPage/_units/clicker.pyi b/DrissionPage/_units/clicker.pyi index efbe999..fd27df5 100644 --- a/DrissionPage/_units/clicker.pyi +++ b/DrissionPage/_units/clicker.pyi @@ -80,14 +80,12 @@ class Clicker(object): save_path: Union[str, Path] = None, rename: str = None, suffix: str = None, - new_tab: bool = False, by_js: bool = False, timeout: float = None) -> DownloadMission: """点击触发下载 :param save_path: 保存路径,为None保存在原来设置的,如未设置保存到当前路径 :param rename: 重命名文件名 :param suffix: 指定文件后缀 - :param new_tab: 该下载是否在新tab中触发 :param by_js: 是否用js方式点击,逻辑与click()一致 :param timeout: 等待下载触发的超时时间,为None则使用页面对象设置 :return: DownloadMission对象 diff --git a/DrissionPage/_units/downloader.py b/DrissionPage/_units/downloader.py index 09125f7..e9a7a4a 100644 --- a/DrissionPage/_units/downloader.py +++ b/DrissionPage/_units/downloader.py @@ -27,7 +27,9 @@ class DownloadManager(object): self._missions = {} # {guid: DownloadMission} self._tab_missions = {} # {tab_id: [DownloadMission, ...]} self._flags = {} # {tab_id: [bool, DownloadMission]} + self._waiting_tab = set() # click.to_download()专用 self._tmp_path = '.' + self._page_id = None self._running = False @@ -97,11 +99,16 @@ class DownloadManager(object): self._tab_missions.pop(tab_id, None) self._flags.pop(tab_id, None) TabDownloadSettings.TABS.pop(tab_id, None) + self._waiting_tab.discard(tab_id) def _onDownloadWillBegin(self, **kwargs): guid = kwargs['guid'] tab_id = self._browser._frames.get(kwargs['frameId'], 'browser') - tab = tab_id if tab_id in TabDownloadSettings.TABS else 'browser' + tab = 'browser' if tab_id in ('browser', self._page_id) else tab_id + opener = self._browser._relation.get(tab_id, None) + from_tab = None + if opener and opener in self._waiting_tab: + tab = from_tab = opener settings = TabDownloadSettings(tab) if settings.rename: @@ -139,16 +146,21 @@ class DownloadManager(object): overwrite = False m = DownloadMission(self, tab_id, guid, settings.path, name, kwargs['url'], self._tmp_path, overwrite) + if from_tab: + m.from_tab = from_tab + self._tab_missions.setdefault(from_tab, []).append(m) self._missions[guid] = m - if self.get_flag(tab) is False: # 取消该任务 + if self.get_flag('browser') is False or self.get_flag(tab) is False: # 取消该任务 self.cancel(m) elif skip: self.skip(m) else: self._tab_missions.setdefault(tab_id, []).append(m) - if self.get_flag(tab) is not None: + if self.get_flag('browser') is not None: + self._flags['browser'] = m + elif self.get_flag(tab) is not None: self._flags[tab] = m def _onDownloadProgress(self, **kwargs): diff --git a/DrissionPage/_units/downloader.pyi b/DrissionPage/_units/downloader.pyi index 37020f2..ef14358 100644 --- a/DrissionPage/_units/downloader.pyi +++ b/DrissionPage/_units/downloader.pyi @@ -18,8 +18,10 @@ class DownloadManager(object): _missions: Dict[str, DownloadMission] = ... _tab_missions: dict = ... _flags: dict = ... + _waiting_tab: set = ... _running: bool = ... _tmp_path: str = ... + _page_id: Optional[str] = ... def __init__(self, browser: Chromium): """ @@ -164,7 +166,7 @@ class DownloadMission(object): name: str, url: str, tmp_path: str, - overwrite:bool): + overwrite: bool): """ :param mgr: BrowserDownloadManager对象 :param tab_id: 标签页id