C# 如何使用 Selenium Webdriver .NET 绑定设置 Chrome 首选项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15824996/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 18:05:05  来源:igfitidea点击:

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

c#google-chromeseleniumwebdriverselenium-webdriver

提问by Yi Zeng

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

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:

取自 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.

检查临时首选项文件*temp\Google\Chrome\User Data\Default\Preferences,没有"default_directory""prompt_for_download"已设置。

   "download": {
      "directory_upgrade": true
   },

采纳答案by Martin Devillers

The Selenium dotNet driver does not support setting the chrome.prefsout of the box. The problem is that chrome.prefsmust be defined as prefsunder the chromeOptionsnode. The ChromeOptionsclass does not contain this variable, so you'll need to create your own ChromeOptionsclass:

Selenium dotNet 驱动程序不支持设置chrome.prefs开箱即用。问题是chrome.prefs必须定义为prefschromeOptions节点。本ChromeOptions类不包含这个变量,所以您需要创建自己ChromeOptions的类:

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

回答by Ben

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

如果您已更新到 Chrome 版本 36.0.x 和 Selenium 2.42,则 Martins 解决方案不再有效。

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

我目前使用它来将我的打印机设置更改为“另存为 PDF”而不是使用此代码作为示例的默认设置

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.

我认为马丁的解决方案非常好和准确,但它突然对我不起作用,所以我自然要看看我是否能找到解决方案。