Denis Koreyba
Denis Koreyba

Reputation: 3718

Error: There is already an option for the mobileEmulation capability. Please use that instead. Parameter name: capabilityName

I've updated Selenium Webdriver C# to the version 2.50.0 and unfortunately I also updated ChromeDriver to the version 2.21 and then I've encountered a problem. I'm inclined to think it's connected with the new version of ChromeDriver but I'm also not sure about new version of Selenium.

I used the next piece of code to run mobile emulation:

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

ChromeOptions options = new ChromeOptions();
options.AddAdditionalCapability("mobileEmulation", mobileEmulation);

And this worked well.

Now on the next string:

options.AddAdditionalCapability("mobileEmulation", mobileEmulation);

It shown me the next error:

There is already an option for the mobileEmulation capability. Please use that instead. Parameter name: capabilityName

So what should I use as the first parameter in this method?

Upvotes: 2

Views: 5037

Answers (2)

JimEvans
JimEvans

Reputation: 27486

The correct thing to do here is to use the EnableMobileEmulation method on the ChromeOptions object. There are two overloads. The first overload takes a string, which is intended to be used with a device name. The second overload takes a ChromeMobileEmulationDeviceSettings object, on which you set things like height, width, and pixel ratio. This method allows the use of type-safe parameters, and which allows you to set the mobileEmulation capability correctly. The code would look something like this:

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

Note: This answer refers to the 2.50.1 release of the .NET bindings, which corrects the API in this area.

Upvotes: 6

Andrew Regan
Andrew Regan

Reputation: 5113

That check was added less than a day ago in Selenium 2.50:

https://github.com/SeleniumHQ/selenium/commit/6db8a5fd2bf8a1fc89d41467d1f21d073ffadfe0

I haven't found any documentation, but it seems you need to use the new type-safe ChromeMobileEmulationDeviceSettings class to set mobile emulation options. Hopefully it's clear enough from the above diff what you need to change.

Upvotes: 2

Related Questions