用selenium操作chrome,有时需要把文件下载到指定目录,从stackoverflow参考下代码,很好用。
private static IWebDriver StartChromedriverWithDefaultdirectory(string downloadDirectory)
{
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory);
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");
string chromePath = String.IsNullOrWhiteSpace(chromeDirectory) ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), @"Google\Chrome\Application")
: chromeDirectory;
return new ChromeDriver(chromePath, chromeOptions);
}
最新版chrome也可以。
本文介绍如何使用Selenium操作Chrome浏览器将文件自动下载至指定目录。通过设置Chrome选项,可以实现下载路径的指定、自动下载及禁用弹窗等功能。
2370

被折叠的 条评论
为什么被折叠?



