这是我将配置文件首选项添加到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和’Capabilities’,通过这种方式,我会将配置文件首选项发送到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;
有人可以帮我吗,我需要像使用Firefox一样对Chrome执行相同的操作。基本上,我需要更改下载文件的默认路径。
您将需要执行以下操作:
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());