如何使用 Selenium Webdriver .NET 绑定设置 Chrome 首选项? [英] How to set Chrome preferences using Selenium Webdriver .NET binding?

查看:29
本文介绍了如何使用 Selenium Webdriver .NET 绑定设置 Chrome 首选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用的,用户代理可以成功设置,而下载首选项不能.

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

取自 chromedriver.log:

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": ""

}

检查 *tempGoogleChromeUser DataDefaultPreferences 处的临时首选项文件,没有 "default_directory""prompt_for_download" 已设置.

Check the temp Preferences file at *tempGoogleChromeUser DataDefaultPreferences, no "default_directory" and "prompt_for_download" are set.

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

推荐答案

Selenium dotNet 驱动程序不支持开箱即用地设置 chrome.prefs.问题是chrome.prefs 必须在chromeOptions 节点下定义为prefs.ChromeOptions 类不包含此变量,因此您需要创建自己的 ChromeOptions 类:

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:pathchromedriver", options);
}

这篇关于如何使用 Selenium Webdriver .NET 绑定设置 Chrome 首选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆