C#设置默认下载目录chrome WebDriver?

时间:2016-02-17 02:40:57

标签: c# google-chrome selenium selenium-webdriver

这是我的解决方案,我参考: How to set Chrome preferences using Selenium Webdriver .NET binding?

但无法正常工作,我需要将谷歌浏览器的默认下载目录更改为

  

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);

        }

3 个答案:

答案 0 :(得分:9)

只是粘贴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 :(得分:4)

这些设置对我有用

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);

答案 2 :(得分:0)

我知道这不是最好的,甚至可能不是最好的方法,但如果您没有找到其他有用的答案,也许会有所帮助。它对我有用:

private static ChromeOptions options()
{
    ChromeOptions chromeOptions = new ChromeOptions();
    chromeOptions.AddUserProfilePreference("download.default_directory", @"path");
    chromeOptions.AddUserProfilePreference("download.prompt_for_download", false);

    return chromeOptions;
    }
     
protected static IWebDriver driver = new ChromeDriver(options());