提问者:小点点

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


这是我的解决方案,基于这个问题

但它不起作用,我需要将google chrome的默认下载目录更改为

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个答案

匿名用户

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

匿名用户

这些设置对我有用

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

匿名用户

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

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