2016-02-17 43 views
7

這是我的解決方案,我在參考: How to set Chrome preferences using Selenium Webdriver .NET binding?C#設置默認的下載目錄chrome WebDriver?

但沒有工作,我需要谷歌瀏覽器更改默認下載目錄

C:\ TEMP \

感謝幫助。

public class ChromeOptionsWithPrefs : ChromeOptions 
    { 
     public Dictionary<string, object> prefs { get; set; } 
    } 

public static void Initialize() 
    { 
     var options = new ChromeOptionsWithPrefs 
     { 
      prefs = new Dictionary<string, object> 
      { 
       {"download.default_directory", @"C:\temp\"} 
      } 
     }; 
     RemoteWebDriver driver = new ChromeDriver(@"D:\chromedriver_win32\", options); 
     var download = driver.FindElements(By.XPath("//a[.='Download']")); 
     foreach (var t in download) 
     { 
      t.SendKeys(Keys.Enter); 
     } 
    } 

林瑤池這種解決方案,它的工作

var chromeOptions = new ChromeOptions(); 
     chromeOptions.AddUserProfilePreference("download.default_directory", @"D:\DataTest"); 
     chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl"); 
     chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); 
     var driver = new ChromeDriver(@"D:\chromedriver_win32\", chromeOptions); 
     var download = driver.FindElements(By.XPath("//a[.='ダウンロード']")); 
     foreach (var t in download) 
     { 
      t.SendKeys(Keys.Enter); 

     } 
+2

你不給的問題,你的答案。只需將其添加爲答案.. – Adarsha

+0

重複的問題在這裏https://stackoverflow.com/questions/33434443/download-file-at-custom-path-using-selenium-webdriver –

回答

6

只需粘貼的是OP找到了答案,但沒有添加作爲一個答案。

var chromeOptions = new ChromeOptions(); 
chromeOptions.AddUserProfilePreference("download.default_directory", @"D:\DataTest"); 
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl"); 
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); 
var driver = new ChromeDriver(@"D:\chromedriver_win32\", chromeOptions); 
var download = driver.FindElements(By.XPath("//a[.='ダウンロード']")); 

foreach (var t in download) 
{ 
    t.SendKeys(Keys.Enter); 
} 
1

這些設置爲我工作

var chromeOptions = new ChromeOptions(); 
var downloadDirectory = "C:\Temp"; 

chromeOptions.AddUserProfilePreference("download.default_directory", downloadDirectory); 
chromeOptions.AddUserProfilePreference("download.prompt_for_download", false); 
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true"); 

var driver = new ChromeDriver(chromeOptions); 
相關問題