用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也可以。