根据需要传递给chromedriver功能c#c#

时间:2016-05-24 09:21:07

标签: c# selenium selenium-chromedriver

我有通过Chrome浏览器进行自动测试的软件,但是现在这个程序失败并显示错误消息“已启用Vector smash protection”。 我已经为这种情况找到了解决方案,但是这个解决方案是通过java API实现的。

DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--incognito"));
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
WebDriver driver = new ChromeDriver(capabilities);

如何通过c#实现与上述相同的代码?

1 个答案:

答案 0 :(得分:2)

你需要一个词典

// Capabilities Values
var imageSetting = new Dictionary<string, object> {{"images", 2}};
var content =      new Dictionary<string, object> {{"profile.default_content_settings", imageSetting}};
var prefs =        new Dictionary<string, object> {{"prefs", content}};

// List of Chromium Command Line Switches 
var options = new ChromeOptions(); 
options.AddArguments( 
     "--disable-extensions", 
     "--disable-features", 
     "--disable-popup-blocking",
     "--disable-settings-window");

// Add the Capabilities
var field = options.GetType().GetField("additionalCapabilities", BindingFlags.Instance | BindingFlags.NonPublic);
if (field != null)
{
     var dict = field.GetValue(options) as IDictionary<string, object>;
     if (dict != null) dict.Add(ChromeOptions.Capability, prefs);
}

// Create the Chrome Driver
var chromeDriver = new ChromeDriver(options);

您还可以在创建ChromeDriver对象时发送驱动程序路径。