Par*_*val 19 python python-2.7 selenium-chromedriver selenium-webdriver
我正在使用Selenium Webdriver(在Python中)自动下载数千个文件.我想以编程方式设置Chrome的下载文件夹.看完这个,我尝试这样做:
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)
Run Code Online (Sandbox Code Playgroud)
不好.下载仍然会转到默认下载文件夹("/ Users/thiagomarzagao/Downloads").
有什么想法吗?
(Python 2.7.5,Selenium 2.2.0,Chromedriver 2.1.210398,Mac OS X 10.6.8)
Mer*_*rvS 50
The following worked for me:
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, chrome_options=chromeOptions)
Run Code Online (Sandbox Code Playgroud)
Source: https://sites.google.com/a/chromium.org/chromedriver/capabilities
yve*_*sva 13
如果有人仍然遇到问题并且上述解决方案无效,我发现在我的下载路径中添加了以下斜杠('\').
我看起来像这样:
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)
Run Code Online (Sandbox Code Playgroud)
小智 10
# -*- 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": "<path to the folder of download>",
"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 = "https://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()
Run Code Online (Sandbox Code Playgroud)
我想你也需要
"directory_upgrade": true
Run Code Online (Sandbox Code Playgroud)
在 Chrome 版本 28.0.1500.95 m 的本地 Windows 安装上直接在 Chrome 的“首选项”文件中使用字典,具有以下下载选项:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"extensions_to_open": ""
},
Run Code Online (Sandbox Code Playgroud)
我得到了默认位置,而不是桌面。当我将其更改为:
"download": {
"default_directory": "C:\\Users\\rdub\\Desktop",
"directory_upgrade": true,
"extensions_to_open": ""
},
Run Code Online (Sandbox Code Playgroud)
我得到桌面位置。
请尝试以下操作:
desired_caps = {'prefs': {'download': {'default_directory': '/Users/thiagomarzagao/Desktop/downloaded_files/', "directory_upgrade": true, "extensions_to_open": ""}}}
Run Code Online (Sandbox Code Playgroud)