From 8dd184e8489a58068f64c17fdc8c8cfb0a2919cc Mon Sep 17 00:00:00 2001 From: g1879 Date: Tue, 17 Oct 2023 15:48:28 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83=E4=B8=8B=E8=BD=BD=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DrissionPage/chromium_driver.py | 3 ++- DrissionPage/setter.py | 6 +++--- DrissionPage/setter.pyi | 6 ++++-- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/DrissionPage/chromium_driver.py b/DrissionPage/chromium_driver.py index 611a767..05aab26 100644 --- a/DrissionPage/chromium_driver.py +++ b/DrissionPage/chromium_driver.py @@ -215,8 +215,9 @@ class BrowserDriver(ChromiumDriver): return object.__new__(cls) def __init__(self, tab_id, tab_type, address): - if tab_id in BrowserDriver.BROWSERS: + if hasattr(self, '_created'): return + self._created = True BrowserDriver.BROWSERS[tab_id] = self super().__init__(tab_id, tab_type, address) diff --git a/DrissionPage/setter.py b/DrissionPage/setter.py index 5677012..54cefe8 100644 --- a/DrissionPage/setter.py +++ b/DrissionPage/setter.py @@ -146,9 +146,9 @@ class TabSetter(ChromiumBaseSetter): """ types = {'rename': 'rename', 'overwrite': 'overwrite', 'skip': 'skip', 'r': 'rename', 'o': 'overwrite', 's': 'skip'} - mode = types.get(mode, None) - if not mode: - raise ValueError(f"mode参数只能是'rename', 'overwrite', 'skip', 'r', 'o', 's' 之一,现在是:{mode}") + mode = types.get(mode, mode) + if mode not in types: + raise ValueError(f'''mode参数只能是 '{"', '".join(types.keys())}' 之一,现在是:{mode}''') self._page.browser._dl_mgr.set_file_exists(self._page.tab_id, mode) diff --git a/DrissionPage/setter.pyi b/DrissionPage/setter.pyi index 8e1aac4..8803ed8 100644 --- a/DrissionPage/setter.pyi +++ b/DrissionPage/setter.pyi @@ -5,7 +5,7 @@ """ from http.cookiejar import Cookie from pathlib import Path -from typing import Union, Tuple +from typing import Union, Tuple, Literal from requests.adapters import HTTPAdapter from requests.auth import HTTPBasicAuth @@ -19,6 +19,8 @@ from .chromium_tab import ChromiumTab from .session_page import SessionPage from .web_page import WebPage +FILE_EXISTS = Literal['skip', 'rename', 'overwrite', 's', 'r', 'o'] + class ChromiumBaseSetter(object): def __init__(self, page): @@ -58,7 +60,7 @@ class TabSetter(ChromiumBaseSetter): def download_file_name(self, name: str) -> None: ... - def when_download_file_exists(self, mode: str) -> None: ... + def when_download_file_exists(self, mode: FILE_EXISTS) -> None: ... class ChromiumPageSetter(TabSetter):