3

这就是我将配置文件首选项添加到 Chrome 以进行本地自动测试运行和 TeamCity(CI) 的方式:

Capabilities = DesiredCapabilities.Chrome();

var chromeOptions = new ChromeOptionsWithPrefs();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

return new ChromeDriver(chromeDriverPath, chromeOptions);

但是当我创建新的“RemoteWebDriver”时,我必须向它发送一个集线器 URL 和“功能”,这样我就可以将配置文件首选项发送到 Firefox(到 RemoteWebDriver):

var profile = new FirefoxProfile();

Capabilities = DesiredCapabilities.Firefox();

profile.SetPreference("browser.helperApps.alwaysAsk.force", false); 
profile.SetPreference("browser.download.useDownloadDir", true);
profile.SetPreference("browser.download.folderList", 2);
profile.SetPreference("browser.download.dir", DownloadPath);
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk",
   "application/zip, application/octet-stream");

Capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile.ToBase64String());

return Capabilities;

有人可以帮助我吗,我需要对 Chrome 做同样的事情,就像我对 Firefox 做的一样。基本上我需要的是我可以更改下载文件的默认路径。

4

1 回答 1

6

您需要执行以下操作:

var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("download.default_directory", DownloadPath);
chromeOptions.AddUserProfilePreference("intl.accept_languages", "nl");
chromeOptions.AddUserProfilePreference("disable-popup-blocking", "true");

IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), chromeOptions.ToCapabilities());
于 2014-03-05T17:54:29.507 回答