无法从Chrome设置默认下载目录 [英] Unable to set default download directory from chrome
问题描述
我在设置chrome驱动程序的默认下载文件夹时遇到问题. 我发现了一些与此有关的信息,但没有一个起作用. 这是我尝试过的:
I have problems with setting the default download folder for chrome driver. I found some information related to this but none of it is working. This is what I've tried:
var options = new ChromeOptionsWithPrefs();
options.AddArguments("start-maximized");
options.prefs = new Dictionary<string, object> {
{ "download.default_directory", folderName },
{ "download.prompt_for_download", false },
{ "intl.accept_languages", "nl" }};
webdriver = new ChromeDriver(chromedriver_path, options);
和
var options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", folderName);
options.AddUserProfilePreference("intl.accept_languages", "nl");
options.AddUserProfilePreference("download.prompt_for_download", "false");
我正在使用chrome驱动程序2.9(最新版本)和chrome版本33. 还尝试为chrome设置默认目录,当我启动Web驱动程序时,我希望默认目录会被更改,但是我做得不好.
I am using chrome driver 2.9(latest one) and chrome version 33. Also tried to set a default directory for chrome and when I start the web-driver I expect that the default directory to be change but I did not work as well.
您对我如何设置此默认文件夹有什么新想法吗?
Do you have any new idea how I can set the this default folder?
添加声明:
string folderName = @"C:\Browser";
推荐答案
在使用ChromeDriver 2.24
和Selenium 3.0
进行此操作时遇到麻烦.
I was running into trouble doing this with ChromeDriver 2.24
and Selenium 3.0
.
对我来说,以下代码有效:
For me the following code worked:
var service = ChromeDriverService.CreateDefaultService(driverPath);
var downloadPrefs = new Dictionary<string, object>
{
{"default_directory", @"C:\Users\underscore\MyCustomLocation"},
{"directory_upgrade", true}
};
var options = new ChromeOptions();
options.AddUserProfilePreference("download", downloadPrefs);
return new ChromeDriver(service, options);
希望这对现在尝试做的人有所帮助.
Hopefully this helps anyone trying to do it now.
以防将来发生变化;我通过打开默认的Chrome偏好设置文件验证了所需的格式.通过浏览到chrome://version
并在配置文件路径"指定的位置打开首选项"文件,可以找到此文件的位置.这表明默认的"download"
键具有一个具有这些值的对象.
In case it changes in future; I verified the required format by opening my default Chrome preferences file. The location of this file can be found by browsing to chrome://version
and opening the Preferences file at the location specified by Profile Path. This showed that the default "download"
key has an object with these values.
然后我可以通过打开Selenium Chrome浏览器使用的首选项文件(再次检查chrome://version
中的位置)来检查是否应用了更改.
I could then check the changes were applied by opening the preferences file used by the Selenium Chrome browser (again by checking the location from chrome://version
).
类似地,为了禁用阻止文件下载的内置Chrome PDF Viewer,我在配置中添加了以下几行:
Similarly in order to disable the inbuilt Chrome PDF Viewer which was blocking file downloads, I added the following lines to the configuration:
var pdfViewerPlugin = new Dictionary<string, object>
{
["enabled"] = false,
["name"] = "Chrome PDF Viewer"
};
var pluginsList = new Dictionary<string, object>
{
{ "plugins_list", new [] { pdfViewerPlugin } }
};
var downloadPreferences = new Dictionary<string, object>
{
{"default_directory", launchOptions.DownloadFolder},
{"directory_upgrade", true}
};
var options = new ChromeOptions();
options.AddUserProfilePreference("download", downloadPreferences);
options.AddUserProfilePreference("plugins", pluginsList);
Firefox
由于我今天在此上浪费了一个小时,因此以下是运行相同版本的Selenium的Firefox(49+)的配置(注意:这不适用于GeckoDriver 0.10.0和Selenium 3.0.0+ ,GeckoDriver必须是0.11.1版):
Firefox
Since I wasted another hour on this today, here is the configuration for Firefox (49+) running the same version of Selenium (Note: this won't work with GeckoDriver 0.10.0 and Selenium 3.0.0+, GeckoDriver must be version 0.11.1):
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "GeckoBinary");
var service = FirefoxDriverService.CreateDefaultService(path);
service.HideCommandPromptWindow = true;
var profile = new FirefoxProfile();
profile.SetPreference("browser.download.dir", myDownloadLocation);
profile.SetPreference("browser.download.downloadDir", myDownloadLocation);
profile.SetPreference("browser.download.defaultFolder", myDownloadLocation);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk", ContentTypes.AllTypesSingleLine);
profile.SetPreference("pdfjs.disabled", true);
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.download.folderList", 2);
return new FirefoxDriver(service, new FirefoxOptions
{
Profile = profile
}, TimeSpan.FromMinutes(5));
ContentTypes.AllTypesSingleLine
只是一个包含mime类型的字符串,例如:
Where ContentTypes.AllTypesSingleLine
is just a string containing mime types, e.g.:
application/pdf;application/excel;...
从GeckoDriver 0.11.1和Selenium 3.0.1开始,可以将其简化为:
As of GeckoDriver 0.11.1 and Selenium 3.0.1 this can be simplified to:
var options = new FirefoxOptions();
options.SetPreference("browser.download.dir", launchOptions.DownloadFolder);
options.SetPreference("browser.download.downloadDir", launchOptions.DownloadFolder);
options.SetPreference("browser.download.defaultFolder", launchOptions.DownloadFolder);
options.SetPreference("browser.helperApps.neverAsk.saveToDisk", ContentTypes.AllTypesSingleLine);
options.SetPreference("pdfjs.disabled", true);
options.SetPreference("browser.download.useDownloadDir", true);
options.SetPreference("browser.download.folderList", 2);
return new FirefoxDriver(service, options, TimeSpan.FromMinutes(5));
这篇关于无法从Chrome设置默认下载目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!