From ce9b17e25dbd767026039b899eb99a450e981fcb Mon Sep 17 00:00:00 2001
From: g1879 <g1879@qq.com>
Date: Wed, 23 Oct 2024 22:46:36 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E8=BD=BD=E9=80=BB?=
 =?UTF-8?q?=E8=BE=91=EF=BC=8C=E5=BE=85=E6=B5=8B=E8=AF=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 DrissionPage/_units/clicker.py     | 13 ++++++++++---
 DrissionPage/_units/downloader.py  |  3 ++-
 DrissionPage/_units/downloader.pyi |  2 +-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/DrissionPage/_units/clicker.py b/DrissionPage/_units/clicker.py
index c277c4a..4a31f54 100644
--- a/DrissionPage/_units/clicker.py
+++ b/DrissionPage/_units/clicker.py
@@ -5,7 +5,6 @@
 @Copyright: (c) 2024 by g1879, Inc. All Rights Reserved.
 @License  : BSD 3-Clause.
 """
-from pathlib import Path
 from time import perf_counter, sleep
 
 from .waiter import wait_mission
@@ -124,6 +123,8 @@ class Clicker(object):
         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'
@@ -133,10 +134,16 @@ class Clicker(object):
             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
@@ -146,8 +153,6 @@ class Clicker(object):
         if save_path:
             tmp_path = obj.download_path
             obj.set.download_path(save_path)
-        else:
-            tmp_path = None
         if rename or suffix:
             obj.set.download_file_name(rename, suffix)
         if timeout is None:
@@ -159,6 +164,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
diff --git a/DrissionPage/_units/downloader.py b/DrissionPage/_units/downloader.py
index 79132b3..09125f7 100644
--- a/DrissionPage/_units/downloader.py
+++ b/DrissionPage/_units/downloader.py
@@ -170,6 +170,7 @@ class DownloadManager(object):
                     to_path = str(get_usable_path(f'{mission.folder}{sep}{mission.name}'))
                 else:
                     to_path = f'{mission.folder}{sep}{mission.name}'
+                Path(mission.folder).mkdir(parents=True, exist_ok=True)
                 not_moved = True
                 for _ in range(10):
                     try:
@@ -206,7 +207,7 @@ class TabDownloadSettings(object):
         self.rename = None
         self.suffix = None
         self.path = '' if tab_id == 'browser' else self.TABS['browser'].path
-        self.when_file_exists = 'rename'
+        self.when_file_exists = 'rename' if tab_id == 'browser' else self.TABS['browser'].when_file_exists
 
         TabDownloadSettings.TABS[tab_id] = self
 
diff --git a/DrissionPage/_units/downloader.pyi b/DrissionPage/_units/downloader.pyi
index b7c66d0..37020f2 100644
--- a/DrissionPage/_units/downloader.pyi
+++ b/DrissionPage/_units/downloader.pyi
@@ -131,7 +131,7 @@ class TabDownloadSettings(object):
     rename: Optional[str] = ...
     suffix: Optional[str] = ...
     path: Optional[str] = ...
-    when_file_exists: str = ...
+    when_file_exists: FILE_EXISTS = ...
 
     def __init__(self, tab_id: str):
         """