Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - How to set Chrome preferences using Selenium Webdriver .NET binding?

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 *tempGoogleChromeUser DataDefaultPreferences, no "default_directory" and "prompt_for_download" are set.

   "download": {
      "directory_upgrade": true
   },
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you have updated to Chrome Version 36.0.x and Selenium 2.42, Martins 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...