Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Tôi đang cố gắng tự động tải xuống một số liên kết thông qua chức năng nhấp chuột của Selenium và tôi đang sử dụng một WebDriver Chrome và Python làm ngôn ngữ lập trình. Làm thế nào tôi có thể chọn thư mục tải xuống thông qua chương trình Python để nó không được tải xuống trong thư mục tải xuống mặc định. Tôi đã tìm thấy một giải pháp cho Firefox nhưng ở đó, hộp thoại tải xuống tiếp tục xuất hiện mỗi khi nó nhấp vào liên kết không xảy ra trong Chrome.chrome webdriver and python as the programming language. How can I select the download directory through the python program so that it does not get downloaded in the default Downloads directory. I found a solution for firefox but there the download dialog keeps popping up every time it clicks on the link which does not happen in Chrome.

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Blaszard

30.2K46 Huy hiệu vàng149 Huy hiệu bạc230 Huy hiệu đồng46 gold badges149 silver badges230 bronze badges

hỏi ngày 11 tháng 2 năm 2016 lúc 5:58Feb 11, 2016 at 5:58

Shubham Gidelshubham GidelShubham Goyal

5971 Huy hiệu vàng6 Huy hiệu bạc18 Huy hiệu đồng1 gold badge6 silver badges18 bronze badges

2

Tôi thấy giải pháp được chấp nhận không hoạt động, tuy nhiên sự thay đổi nhỏ này đã làm:

from selenium import webdriver

chrome_options = webdriver.ChromeOptions()
prefs = {'download.default_directory' : '/path/to/dir'}
chrome_options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(chrome_options=chrome_options)

Đã trả lời ngày 4 tháng 5 năm 2017 lúc 17:51May 4, 2017 at 17:51

10

Cập nhật 2018:

Công tắc dòng lệnh chrome không hợp lệ của nó, xem mã nguồn Sử dụng câu trả lời HOJU bên dưới để đặt các tùy chọn.

Original:

Bạn có thể tạo một hồ sơ cho Chrome và xác định vị trí tải xuống cho các thử nghiệm. Đây là một ví dụ:

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)

Ewwink

17.7K2 Huy hiệu vàng42 Huy hiệu bạc54 Huy hiệu đồng2 gold badges42 silver badges54 bronze badges

Đã trả lời ngày 11 tháng 2 năm 2016 lúc 7:48Feb 11, 2016 at 7:48

6

Vấn đề chính xác tôi cũng gặp phải trong khi cố gắng làm chính xác những gì bạn muốn :)

Đối với Chrome:chrome:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
prefs = {"profile.default_content_settings.popups": 0,
             "download.default_directory": 
                        r"C:\Users\user_dir\Desktop\\",#IMPORTANT - ENDING SLASH V IMPORTANT
             "directory_upgrade": True}
options.add_experimental_option("prefs", prefs)
browser=webdriver.Chrome(, options=options)

Đối với Firefox: Theo dõi blog này để biết câu trả lời: https://srirajeshsahoo.wordpress.com/2018/07/26/how-to-bypass-pop-up-during-xuống-in-firefox-using-selenium-and- Python/Firefox: follow this blog for the answer: https://srirajeshsahoo.wordpress.com/2018/07/26/how-to-bypass-pop-up-during-download-in-firefox-using-selenium-and-python/

Blog cho biết tất cả về Dir Pop-up và tải xuống và cách làm

Đã trả lời ngày 15 tháng 8 năm 2019 lúc 14:16Aug 15, 2019 at 14:16

Sử dụng prefs đã giải quyết vấn đề của tôi

path = os.path.dirname(os.path.abspath(__file__))
prefs = {"download.default_directory":path}
options = Options()
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('../harveston/chromedriver.exe',options = options)

Đã trả lời ngày 31 tháng 7 năm 2020 lúc 10:21Jul 31, 2020 at 10:21

Điều này làm việc cho tôi trên Chrome

from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Downloads")

driver = webdriver.Chrome(chrome_options=options)
0

preferences = {
                "profile.default_content_settings.popups": 0,
                "download.default_directory": os.getcwd() + os.path.sep,
                "directory_upgrade": True
            }

chrome_options.add_experimental_option('prefs', preferences)
browser = webdriver.Chrome(executable_path="/usr/bin/chromedriver", options=chrome_options)

Đã trả lời ngày 8 tháng 5 năm 2020 lúc 23:01May 8, 2020 at 23:01

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

CodewalkerCodewalkerCodeWalker

2.1734 Huy hiệu vàng20 Huy hiệu bạc44 Huy hiệu đồng4 gold badges20 silver badges44 bronze badges

4

Cập nhật 2022:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = Options()

prefs = {"download.default_directory" : "C:\YourDirectory\Folder"}

options.add_experimental_option("prefs", prefs)

driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)

Đã trả lời ngày 1 tháng 8 lúc 13:19Aug 1 at 13:19

1

Để cung cấp thư mục tải xuống và đường dẫn thực thi Diver của Chrome, sử dụng mã sau.

from selenium import webdriver
options = webdriver.ChromeOptions() 
options.add_argument("download.default_directory=C:/Your_Directory")
driver = webdriver.Chrome(options=options ,executable_path='C:/chromedriver')

Thay đổi đường dẫn trong mã của bạn cho phù hợp.

Đã trả lời ngày 4 tháng 6 năm 2019 lúc 6:11Jun 4, 2019 at 6:11

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Nếu bạn đang sử dụng phân phối Linux

Sử dụng mã này

prefs = {'download.prompt_for_download': False,
         'download.directory_upgrade': True,
         'safebrowsing.enabled': False,
         'safebrowsing.disable_download_protection': True}

options.add_argument('--headless')
options.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome('chromedriver.exe', chrome_options=options)
driver.command_executor._commands["send_command"] = ("POST", '/session/$sessionId/chromium/send_command')
driver.desired_capabilities['browserName'] = 'ur mum'
params = {'cmd': 'Page.setDownloadBehavior', 'params': {'behavior': 'allow', 'downloadPath': r'C:\chickenbutt'}}
driver.execute("send_command", params)

Gdorn

8.2816 Huy hiệu vàng36 Huy hiệu bạc37 Huy hiệu Đồng6 gold badges36 silver badges37 bronze badges

Đã trả lời ngày 28 tháng 9 năm 2018 lúc 2:47Sep 28, 2018 at 2:47

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Alex Montoyaalex MontoyaAlex Montoya

4.3651 Huy hiệu vàng27 Huy hiệu bạc29 Huy hiệu đồng1 gold badge27 silver badges29 bronze badges

Tôi thấy rằng nhiều người có cùng một vấn đề, chỉ cần thêm dấu gạch chéo ngược ở cuối

op = webdriver.ChromeOptions()
prefs = {'download.default_directory' : 'C:\\Users\\SAJComputer\\PycharmProjects\\robot-test\\'}
op.add_experimental_option('prefs', prefs)
driver = webdriver.Chrome(executable_path=driver_path , options=op)

Đã trả lời ngày 26 tháng 9 năm 2021 lúc 16:41Sep 26, 2021 at 16:41

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

1

Đoạn mã dưới đây giữ tốt cho Windows/Linux/MacOS Distro:

    downloadDir = f"{os.getcwd()}//downloads//"
    # Make sure path exists.
    Path(downloadDir).mkdir(parents=True, exist_ok=True)
    
    # Set Preferences.
    preferences = {"download.default_directory": downloadDir,
                   "download.prompt_for_download": False,
                   "directory_upgrade": True,
                   "safebrowsing.enabled": True}

    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument("--window-size=1480x560")
    chromeOptions.add_experimental_option("prefs", preferences)

    driver = webdriver.Chrome(DRIVER_PATH, options=chromeOptions)
    driver.get(url)
    time.sleep(10)
    driver.close()

Đã trả lời ngày 23 tháng 11 năm 2021 lúc 17:27Nov 23, 2021 at 17:27

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Abhishake Guptaabhishake GuptaAbhishake Gupta

2.6511 Huy hiệu vàng24 Huy hiệu bạc33 Huy hiệu đồng1 gold badge24 silver badges33 bronze badges

Đây là giải pháp cấp độ mã không có cài đặt định hình/tùy chọn Chrome.

Nếu bạn chỉ sử dụng tập lệnh trên máy cục bộ thì hãy sử dụng giải pháp này

Nhấp vào menu -> Cài đặt -> Hiển thị cài đặt nâng cao ... -> Tải xuống

Bây giờ bỏ chọn

Hỏi nơi lưu mỗi tệp trước khi tải xuống

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Hy vọng nó sẽ giúp bạn :)

Đã trả lời ngày 11 tháng 2 năm 2016 lúc 6:23Feb 11, 2016 at 6:23

Hướng dẫn how do i set the download path in python? - làm cách nào để đặt đường dẫn tải xuống trong python?

Shubham Jainshubham JainShubham Jain

15.6K12 Huy hiệu vàng76 Huy hiệu bạc118 Huy hiệu đồng12 gold badges76 silver badges118 bronze badges

7