From 99dfaf91da5443f1c3a0237971370ba19810fb8f Mon Sep 17 00:00:00 2001 From: g1879 Date: Sun, 5 Nov 2023 23:05:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbrowser=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/_base/browser.py | 7 +++---- DrissionPage/_base/browser.pyi | 2 +- DrissionPage/_units/download_manager.py | 15 ++++++++++++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/DrissionPage/_base/browser.py b/DrissionPage/_base/browser.py index 35ed1f0..5593260 100644 --- a/DrissionPage/_base/browser.py +++ b/DrissionPage/_base/browser.py @@ -47,16 +47,15 @@ class Browser(object): self._process_id = i['id'] break - self.run_cdp('Target.setDiscoverTargets') + self.run_cdp('Target.setDiscoverTargets', discover=True) self._driver.set_listener('Target.targetDestroyed', self._onTargetDestroyed) def _onTargetDestroyed(self, **kwargs): """标签页关闭时执行""" tab_id = kwargs['targetId'] self._dl_mgr.clear_tab_info(tab_id) - for k, i in self._frames.items(): - if i == tab_id: - self._frames.pop(k) + for item in [(k, i) for k, i in self._frames.items() if i == tab_id]: + self._frames.pop(item[0]) def connect_to_page(self): """执行与page相关的逻辑""" diff --git a/DrissionPage/_base/browser.pyi b/DrissionPage/_base/browser.pyi index 8146d35..ee5edad 100644 --- a/DrissionPage/_base/browser.pyi +++ b/DrissionPage/_base/browser.pyi @@ -50,6 +50,6 @@ class Browser(object): def connect_to_page(self) -> None: ... - def _onTargetDestroyed(self, **kwargs): ... + def _onTargetDestroyed(self, **kwargs) -> None: ... def quit(self) -> None: ... diff --git a/DrissionPage/_units/download_manager.py b/DrissionPage/_units/download_manager.py index 0010119..0fbd75f 100644 --- a/DrissionPage/_units/download_manager.py +++ b/DrissionPage/_units/download_manager.py @@ -127,9 +127,18 @@ class BrowserDownloadManager(object): :param tab_id: 标签页id :return: None """ - self._tab_missions.pop(tab_id) - self._flags.pop(tab_id) - TabDownloadSettings.TABS.pop(tab_id) + try: + self._tab_missions.pop(tab_id) + except KeyError: + pass + try: + self._flags.pop(tab_id) + except KeyError: + pass + try: + TabDownloadSettings.TABS.pop(tab_id) + except KeyError: + pass def _onDownloadWillBegin(self, **kwargs): """用于获取弹出新标签页触发的下载任务"""