如何使用Selenium的webdriver .NET中的绑定设置Chrome的preferences? [英] How to set Chrome preferences using Selenium Webdriver .NET binding?

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

问题描述

下面是我使用的是什么,用户代理能够建立成功,而下载preferences不能。

Windows 7中,26铬,硒 - DOTNET-2.31.2,chromedriver_win_26.0.1383.0

ChromeOptions chromeOptions =新ChromeOptions();
VAR preFS =新词典<字符串对象> {
    {download.default_directory,@C:\\ code},
    {download.prompt_for_download,假}
};
chromeOptions.AddAdditionalCapability(,preFS铬preFS。);
chromeOptions.AddArgument( - 用户代理=+一些野生动物园剂);
VAR司机=新ChromeDriver(chromeOptions);

这chromedriver.log摘自:

  [1.201] [精细]:有能力的初始化会话{   browserName:铬   铬preFS:{      download.default_directory:C:\\\\ code,      download.prompt_for_download:假的   },   chrome.switches:[--user-剂=的Mozilla / 5.0(Windows NT的6.1; WOW64)为AppleWebKit / 534.57.2(KHTML,例如Gecko)版本......]   chromeOptions:{      ARGS:[--user-剂=的Mozilla / 5.0(Windows NT的6.1; WOW64)为AppleWebKit / 534.57.2(KHTML,例如Gecko)版本......]      二进制:,      扩展:[]   },   javascriptEnabled:​​真实,   平台:WINDOWS   版本:}

检查临时preferences文件在 * TEMP \\谷歌\\铬\\用户数据\\ DEFAULT \\ preferences ,没有 default_directoryprompt_for_download设置。

 下载:{
      directory_upgrade:真
   },


解决方案

硒dotNet的驱动程序不支持设置镀铬。preFS 开箱。问题是,镀铬。preFS 在<$ C $下必须定义为 preFS C> chromeOptions 节点。在 ChromeOptions 类不包含这个变量,所以您需要创建自己的 ChromeOptions 类:

 公共类ChromeOptionsWith preFS:ChromeOptions
{
    公共字典&LT;字符串对象&gt; $ P $ {PFS得到;组; }
}公共静态无效初始化()
{
    VAR的选择=新ChromeOptionsWith preFS();
    选项​​preFS =新词典&LT;字符串对象&gt;
    {
        {intl.accept_languages​​,NL}
    };
    _driver =新ChromeDriver(@C:\\路径\\ chromedriver选项);
}

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
   },

解决方案

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

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

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