【问题标题】:C# Set default download directory chrome WebDriver?C#设置默认下载目录chrome WebDriver?
【发布时间】:2016-05-28 13:52:42
【问题描述】:

这是我的解决方案,我参考: 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);

        }

【问题讨论】:

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


【解决方案1】:

只是粘贴 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);
}

【讨论】:

    【解决方案2】:

    这些设置对我有用

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 2018-07-26
        • 1970-01-01
        • 2020-01-28
        • 1970-01-01
        • 2016-04-03
        • 2019-12-01
        • 1970-01-01
        • 2022-11-11
        相关资源
        最近更新 更多