错误:mobileEmulation功能已经存在一个选项。请改用它。参数名称:capabilityName

Den*_*yba 2 c# selenium-chromedriver selenium-webdriver

我已经将Selenium Webdriver C#更新到版本2.50.0,不幸的是,我也更新ChromeDriver到版本2.21,然后遇到了问题。我倾向于认为它与ChromeDriverSelenium 的新版本有关,但我也不确定Selenium的新版本。

我使用了下一段代码来运行移动仿真:

var mobileEmulation = new Dictionary<string, string>
{
     {"deviceName", device}
};

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("mobileEmulation", mobileEmulation);
Run Code Online (Sandbox Code Playgroud)

而且效果很好。

现在在下一个字符串上:

options.AddAdditionalCapability("mobileEmulation", mobileEmulation);
Run Code Online (Sandbox Code Playgroud)

它显示了下一个错误:

mobileEmulation功能已经存在一个选项。请改用它。参数名称:capabilityName

那么,该方法中的第一个参数应该是什么?

Jim*_*ans 5

正确的做法是EnableMobileEmulationChromeOptions对象上使用方法。有两个重载。第一个重载采用一个字符串,该字符串应与设备名称一起使用。第二次重载使用一个ChromeMobileEmulationDeviceSettings对象,在该对象上设置高度,宽度和像素比率之类的内容。此方法允许使用类型安全的参数,并允许您mobileEmulation正确设置功能。代码看起来像这样:

// Assumes deviceName is a string variable containing the name
// of the device to emulate.
ChromeOptions options = new ChromeOptions();
options.EnableMobileEmulation(deviceName);
Run Code Online (Sandbox Code Playgroud)

注意:此答案是指.NET绑定的2.50.1版本,此版本更正了此区域中的API。