【问题标题】:"ChromeOptions.AddUserProfilePreference" updating in a wrong format in Selenium C#“ChromeOptions.AddUserProfilePreference”在 Selenium C# 中以错误的格式更新
【发布时间】:2020-07-20 01:12:31
【问题描述】:

我想更新 Selenium C# 中的 Chrome 配置文件首选项,以通过添加以下行作为 Chrome 选项的一部分来使弹出窗口消失。

string url = "https://google.com";
string envName = "xyz.qa";

ChromeOptions.AddUserProfilePreference("protocol_handler.allowed_origin_protocol_pairs." + url + "." + envName, true);

但是,上面的行导致以错误的格式更新 chrome 配置文件首选项文件,即,当它在首选项名称中遇到 .(dot) 时,它不会被抑制。

预期: "protocol_handler":{"allowed_origin_protocol_pairs":{"https://google.com":{"xyz.qa":true}}}

实际: "protocol_handler":{"allowed_origin_protocol_pairs":{"https://google" {"com":{"xyz" {"qa":true}}}}}

第二种方法:

string jsonValue = "{ " + url + ":{ " + envName + ":true} }";
var obj = JObject.Parse(jsonValue);
ChromeOptions.AddUserProfilePreference("protocol_handler.allowed_origin_protocol_pairs", obj);

预期:"protocol_handler":{"allowed_origin_protocol_pairs":{"https://google.com":{"xyz.qa":true}}}

实际:"protocol_handler":{"allowed_origin_protocol_pairs":{"https://google.com" :{"xyz.qa":[] }}}

一切看起来都不错,除了值 true,它被替换为 []。

我尝试了不同的方法来更正格式,但无法更正。请建议我如何以预期格式更新首选项文件。

【问题讨论】:

  • 这方面有什么帮助吗?
  • 对此有何建议?

标签: c# selenium google-chrome chrome-options


【解决方案1】:

终于,当 url 或应用名称包含 . (点)。希望这可以帮助某人通过代码处理 Chrome 安全弹出窗口。

var first = new Dictionary<string, object> { };
var second = new Dictionary<string, object> { };
options.prefs = new Dictionary<string, object> { };

first.Add("<<ApplicationName>>." + "<<Environment>>", false);
second.Add("<<ApplicationURL>>", first);
options.AddUserProfilePreference("protocol_handler.allowed_origin_protocol_pairs", second);

【讨论】:

    猜你喜欢
    • 2019-01-12
    • 1970-01-01
    • 2016-12-14
    • 2020-09-30
    • 1970-01-01
    • 1970-01-01
    • 2013-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多