在 Selenium 2.39 及之前的版本中使用 c#,您可以直接与 ChromeOptions 和 DesiredCapability 交互。因此,如果您想在现代版本的 Windows 上模拟 Android 设备,您的代码片段可能如下所示:
// Define the ChromeOptions to make Chrome act like a mobile device
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-agent=Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
options.AddArgument("--disable-sync-passwords");
Capabilities = DesiredCapabilities.Chrome();
//Needed to find Win7 VM rather than Linux or Mac
Capabilities.SetCapability("platform", "VISTA");
//CEF apps are also tested using Selenium2 and Grid.
//A version of "real" has been created in my Grid config to ensure
//I target current Chrome and not CEF.
Capabilities.SetCapability("version", "real");
Capabilities.SetCapability(ChromeOptions.Capability, options);
//Get a new RemoteWebDriver that thinks it is an Android device
Driver = new RemoteWebDriver(new Uri(Settings.RemoteWebDriverSettings.GridLocation), Capabilities);
//Resize for mobile
Driver.Manage().Window.Position = new System.Drawing.Point(0, 0);
Driver.Manage().Window.Size = new System.Drawing.Size(400, 680);
但是,在 Selenium 2.40 及更高版本中,进行了一项更改,打破了 .net 的这种方法。我能找到的对变化的最好解释来自 this exchange here其中指出:
The .NET bindings are moving toward a pattern where DesiredCapabilites should not be used directly, even with RemoteWebDriver. To facilitate that, the ChromeOptions class has a ToCapabilities() method.
尽我所能,我再也找不到如何在使用 ChromeOptions 时设置目标平台和版本的好示例。
在某些时候,我想利用最新版本的 ChromeDriver 中引入的新 MobileEmulation,但我必须首先克服这个障碍。
任何帮助将不胜感激。
最佳答案
从 Selenium 项目成员那里得到的答复是 can be seen here .
我上面的代码片段会变成这样:
// Define the ChromeOptions to make Chrome act like a mobile device
ChromeOptions options = new ChromeOptions();
options.AddArgument("--user-agent=Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19");
options.AddArgument("--disable-sync-passwords");
//You can cast the ICapabilities object returned by ToCapabilities() as DesiredCapabilities
capabilities = options.ToCapabilities() as DesiredCapabilities;
//Needed to find Win7 VM rather than Linux or Mac
capabilities.SetCapability("platform", "VISTA");
//CEF apps are also tested using Selenium2 and Grid.
//A version of "real" has been created in my Grid config to ensure
//I target current Chrome and not CEF.
capabilities.SetCapability("version", "real");
//Get a new RemoteWebDriver that thinks it is an Android device
Driver = new RemoteWebDriver(new Uri(Settings.RemoteWebDriverSettings.GridLocation), capabilities);
//Resize for mobile
Driver.Manage().Window.Position = new System.Drawing.Point(0, 0);
Driver.Manage().Window.Size = new System.Drawing.Size(400, 680);
关于c# - 自最近更新以来,对如何在将 ChromeOptions for RemoteWebDriver 与 Selenium donNet 结合使用时指定平台和版本感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589560/