47
Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.
Tôi đang sử dụng Selenium WebDriver [bằng Python] để tự động hóa việc tải xuống hàng ngàn tệp. Tôi muốn đặt thư mục tải xuống của Chrome theo chương trình. Sau khi đọc nó, tôi đã thử điều này:
chromepath = '/Users/thiagomarzagao/Desktop/searchcode/chromedriver'
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/'}}}
driver = webdriver.Chrome[executable_path = chromepath, desired_capabilities = desired_caps]
Không tốt. Tải xuống vẫn được chuyển đến thư mục tải xuống mặc định ["/người dùng/Thiagomarzagao/Tải xuống"].
Bất kỳ suy nghĩ?
[Python 2.7.5, Selenium 2.2.0, Chromedriver 2.1.210398, Mac OS X 10.6.8]
Jareq
Phù hiệu 71 Bạc7 Huy hiệu Đồng1 silver badge7 bronze badges
Hỏi ngày 2 tháng 8 năm 2013 lúc 21:26Aug 2, 2013 at 21:26
Những điều sau đây làm việc cho tôi:
chromeOptions = webdriver.ChromeOptions[]
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option["prefs",prefs]
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome[executable_path=chromedriver, options=chromeOptions]
Nguồn: //sites.google.com/a/chromium.org/chromedriver/capabilities
Áp -ra -ham
6,8403 Huy hiệu vàng40 Huy hiệu bạc51 Huy hiệu Đồng3 gold badges40 silver badges51 bronze badges
Đã trả lời ngày 26 tháng 9 năm 2013 lúc 9:46Sep 26, 2013 at 9:46
MervsmervsMervS
5.4163 Huy hiệu vàng22 Huy hiệu bạc35 Huy hiệu Đồng3 gold badges22 silver badges35 bronze badges
7
Nếu bất cứ ai vẫn gặp rắc rối và các giải pháp trên không hoạt động, tôi thấy việc thêm một dấu gạch chéo sau ['\'] vào đường dẫn tải xuống của tôi.
Của tôi trông như thế này:
if browser == 'chrome':
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]
return webdriver.Chrome[executable_path=Base.chromedriver_dir, chrome_options=options]
Đã trả lời ngày 22 tháng 3 năm 2017 lúc 5:41Mar 22, 2017 at 5:41
Yvesvayvesvayvesva
6717 Huy hiệu bạc10 Huy hiệu đồng7 silver badges10 bronze badges
9
Tôi nghĩ bạn cũng cần
"directory_upgrade": true
Sử dụng từ điển trực tiếp trong tệp 'prefences' chrome, trên cài đặt Windows cục bộ của phiên bản Chrome 28.0.1500.95 m, với các tùy chọn tải xuống sau:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"extensions_to_open": ""
},
Tôi nhận được vị trí mặc định, so với máy tính để bàn. Khi tôi thay đổi nó thành điều này:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"directory_upgrade": true,
"extensions_to_open": ""
},
Tôi nhận được vị trí máy tính để bàn.
Hãy thử những điều sau:
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}
Đã trả lời ngày 2 tháng 8 năm 2013 lúc 22:23Aug 2, 2013 at 22:23
R Dubr DubR Dub
6686 Huy hiệu bạc23 Huy hiệu Đồng6 silver badges23 bronze badges
7
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
temp_directory = ""
chrome_options = Options[]
chrome_options.add_argument["--headless"]
chrome_options.add_argument["--window-size=1920x1080"]
chrome_options.add_argument["--disable-notifications"]
chrome_options.add_argument['--no-sandbox']
chrome_options.add_argument['--verbose']
chrome_options.add_experimental_option["prefs", {
"download.default_directory": "",
"download.prompt_for_download": False,
"download.directory_upgrade": True,
"safebrowsing_for_trusted_sources_enabled": False,
"safebrowsing.enabled": False
}]
chrome_options.add_argument['--disable-gpu']
chrome_options.add_argument['--disable-software-rasterizer']
url = "//www.thinkbroadband.com/download"
driver = webdriver.Chrome[executable_path = './chromedriver' ,chrome_options = chrome_options]
driver.get[url]
time.sleep[5]
driver.find_element_by_css_selector["div.module:nth-child[8] > p:nth-child[1] > a:nth-child[1] > img:nth-child[1]"].click[]
Đã trả lời ngày 7 tháng 4 năm 2021 lúc 20:50Apr 7, 2021 at 20:50
3
Tôi đã thử tất cả các anwsers trong câu hỏi này, nhưng nó không hoạt động cho tôi trong Ubuntu 16.10. Vì vậy, tôi thêm thay đổi với Os.EnViron cho biến xdg_doad_dir. Mà không hoạt động, nhưng tôi nghĩ rằng nó có ích.
Đó là:
os.environ['XDG_DOWNLOAD_DIR'] = default_download_directory
Sự thay đổi thực sự hoạt động hoàn hảo đối với tôi là thiết lập thư mục tải xuống thông qua lệnh xdg-user-dirs-update thông qua cuộc gọi hệ thống trong thời gian thực hiện:works perfectly for me is setup the download folder via the command xdg-user-dirs-update through a system call in execution time:
os.system["xdg-user-dirs-update --set DOWNLOAD " + default_download_directory]
Vì vậy, tất cả các mã của tôi liên quan đến việc thiết lập DIR tải xuống là như sau:
chromeOptions = webdriver.ChromeOptions[]
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option["prefs",prefs]
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome[executable_path=chromedriver, options=chromeOptions]
0
RESCDSK
8,5904 Huy hiệu vàng34 Huy hiệu bạc32 Huy hiệu đồng4 gold badges34 silver badges32 bronze badges
Đã trả lời ngày 29 tháng 1 năm 2017 lúc 12:20Jan 29, 2017 at 12:20
Shakaranshakaranshakaran
10.3k2 Huy hiệu vàng28 Huy hiệu bạc46 Huy hiệu đồng2 gold badges28 silver badges46 bronze badges
2
Đối với bất kỳ ai vẫn tự hỏi tại sao việc triển khai của họ không hoạt động: bạn phải đưa con đường đầy đủ cho nó hoạt động. ví dụ. '/Người dùng/bạn/dlfolder' sẽ không hoạt động, trong khi 'c:/user/bạn/dlfolder' sẽ.
Đã trả lời ngày 24 tháng 2 năm 2018 lúc 21:32Feb 24, 2018 at 21:32
Chuyển biến cho "download.default_directory"
Lưu trữ đường dẫn DIR trong biến và chuyển biến sang "download.default_directory"
Lưu ý: Cả .PY Tệp và thư mục "PDF_Folder" ở cùng một vị trí và tệp sẽ tải xuống trong thư mục "PDF_Folder" Both .py file and Folder "PDF_Folder" in same location and file should download in Folder "PDF_Folder"
chromeOptions = webdriver.ChromeOptions[]
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option["prefs",prefs]
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome[executable_path=chromedriver, options=chromeOptions]
1Đã trả lời ngày 3 tháng 9 năm 2020 lúc 16:04Sep 3, 2020 at 16:04
Điều này có vẻ phù hợp với tôi
chromeOptions = webdriver.ChromeOptions[]
prefs = {"download.default_directory" : "/some/path"}
chromeOptions.add_experimental_option["prefs",prefs]
chromedriver = "path/to/chromedriver.exe"
driver = webdriver.Chrome[executable_path=chromedriver, options=chromeOptions]
2
Đã trả lời ngày 18 tháng 7 năm 2021 lúc 20:05Jul 18, 2021 at 20:05
Greg W.F.Rgreg W.F.R.Greg W.F.R
3341 Huy hiệu vàng3 Huy hiệu bạc12 Huy hiệu đồng1 gold badge3 silver badges12 bronze badges