How to set Chrome preferences using Selenium Webdriver .NET binding?
Asked Answered
R

2

18

Here is what I'm using, user agent can be successfully set, while download preferences cannot.

Windows 7, Chrome 26, Selenium-dotnet-2.31.2, chromedriver_win_26.0.1383.0

ChromeOptions chromeOptions = new ChromeOptions();
var prefs = new Dictionary<string, object> {
    { "download.default_directory", @"C:\code" },
    { "download.prompt_for_download", false }
};
chromeOptions.AddAdditionalCapability("chrome.prefs", prefs);
chromeOptions.AddArgument("--user-agent=" + "some safari agent");
var driver = new ChromeDriver(chromeOptions);

Taken from chromedriver.log:

[1.201][FINE]:      Initializing session with capabilities {

   "browserName": "chrome",

   "chrome.prefs": {

      "download.default_directory": "C:\\code",

      "download.prompt_for_download": false

   },

   "chrome.switches": [ "--user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version..." ],

   "chromeOptions": {

      "args": [ "--user-agent=Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.57.2 (KHTML, like Gecko) Version..." ],

      "binary": "",

      "extensions": [  ]

   },

   "javascriptEnabled": true,

   "platform": "WINDOWS",

   "version": ""

}

Check the temp Preferences file at *temp\Google\Chrome\User Data\Default\Preferences, no "default_directory" and "prompt_for_download" are set.

   "download": {
      "directory_upgrade": true
   },
Reset answered 5/4, 2013 at 2:39 Comment(0)
F
23

The Selenium dotNet driver does not support setting the chrome.prefs out of the box. The problem is that chrome.prefs must be defined as prefs under the chromeOptions node. The ChromeOptions class does not contain this variable, so you'll need to create your own ChromeOptions class:

public class ChromeOptionsWithPrefs: ChromeOptions
{
    public Dictionary<string,object> prefs { get; set; }
}

public static void Initialize()
{
    var options = new ChromeOptionsWithPrefs();
    options.prefs = new Dictionary<string, object>
    {
        { "intl.accept_languages", "nl" }
    };
    _driver = new ChromeDriver(@"C:\path\chromedriver", options);
}
Freitag answered 12/8, 2013 at 19:53 Comment(5)
Works like a charm. Thank you. How did you know this? Any references?Reset
I learned from code.google.com/p/chromedriver/wiki/… that the prefs should be part of chromeOptions. Then I reverse-engineered the Selenium .NET driver with JetBrains dotPeek to see how it passed options to chromedriver.exe. Apparently ChromeOptions is quite literraly a wrapper around the chromeOptions-object chromedriver expects. The .NET library serializes the ChromeOptions to JSON and passes it to chromedriver. By inheriting from ChromeOptions and adding your own properties you can add more options :-)Freitag
Bravo, Martin! This worked wonderfully! A small disclaimer for readers, although .NET styling and consistency rules demand for Properties to be capitalized, do not capitalize the prefs property. Otherwise, the Chrome Driver will not be able to parse it correctly and will through an error upon being called.Disparagement
For those who can't get this to work, see Ben's solution below.Wyon
If this answer no longer works, shouldn't it be removed, or flagged up?Albaalbacete
F
33

If you have updated to Chrome Version 36.0.x and Selenium 2.42, Martin's solution no longer works.

It seems to have been updated. You now can use the following code

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference(string preferenceName, object preferenceValue); 

I currently use it to change my Printer settings to "Save as PDF" instead of the default using this code as an example

ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("printing.print_preview_sticky_settings.appState", "{\"version\":2,\"isGcpPromoDismissed\":false,\"selectedDestinationId\":\"Save as PDF\");

I thought Martin's solution was very good and accurate, but it suddenly stopped working for me, so naturally I had to see if I could find a solution.

Fervency answered 1/8, 2014 at 23:27 Comment(1)
This works like a charm for latest chrome. You make my day.. :)Classieclassification
F
23

The Selenium dotNet driver does not support setting the chrome.prefs out of the box. The problem is that chrome.prefs must be defined as prefs under the chromeOptions node. The ChromeOptions class does not contain this variable, so you'll need to create your own ChromeOptions class:

public class ChromeOptionsWithPrefs: ChromeOptions
{
    public Dictionary<string,object> prefs { get; set; }
}

public static void Initialize()
{
    var options = new ChromeOptionsWithPrefs();
    options.prefs = new Dictionary<string, object>
    {
        { "intl.accept_languages", "nl" }
    };
    _driver = new ChromeDriver(@"C:\path\chromedriver", options);
}
Freitag answered 12/8, 2013 at 19:53 Comment(5)
Works like a charm. Thank you. How did you know this? Any references?Reset
I learned from code.google.com/p/chromedriver/wiki/… that the prefs should be part of chromeOptions. Then I reverse-engineered the Selenium .NET driver with JetBrains dotPeek to see how it passed options to chromedriver.exe. Apparently ChromeOptions is quite literraly a wrapper around the chromeOptions-object chromedriver expects. The .NET library serializes the ChromeOptions to JSON and passes it to chromedriver. By inheriting from ChromeOptions and adding your own properties you can add more options :-)Freitag
Bravo, Martin! This worked wonderfully! A small disclaimer for readers, although .NET styling and consistency rules demand for Properties to be capitalized, do not capitalize the prefs property. Otherwise, the Chrome Driver will not be able to parse it correctly and will through an error upon being called.Disparagement
For those who can't get this to work, see Ben's solution below.Wyon
If this answer no longer works, shouldn't it be removed, or flagged up?Albaalbacete

© 2022 - 2024 — McMap. All rights reserved.