我在 C# 上修改 FireFox 浏览器时遇到一些问题。 当浏览器打开时出现错误:
TypeError: Given browserName [object String] "firefox", but my name is [object String] "anotherbrowser"
我认为,这是因为浏览器的二进制代码中的名称是“anotherbrowser”而不是“firefox”。
所以我在Java上找到了解决方案:
FirefoxOptions options = new FirefoxOptions();
options.setBinary("Path to browser binary");
options.setCapability("browserName", "anotherbrowser");
options.setCapability("marionette", false);
driver = new FirefoxDriver(options);
我测试了一下,效果很好。但我需要C#,所以我尝试重写它:
DesiredCapabilities cap = DesiredCapabilities.Firefox();
cap.SetCapability("browserName", "anotherbrowser");
cap.SetCapability("firefox_binary", @"Path to browser binary");
IWebDriver driver = new FirefoxDriver(cap); //here's error
但是我得到了一个错误:
There is already an option for the browserName capability. Please use the instead.
请帮我将 Java 解决方案重写为 C#。我遇到了麻烦,因为 C# 中没有与 Java 中相同的方法:"options.setCapability("","");"
<小时/>P.s.我无法使用原始的 Firefox 浏览器,因为它没有我修改后的 Firefox 相同的优点。
最佳答案
您是否尝试过像在 Java 中一样使用选项类?
在 C# 中你可以使用这样的东西:
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = @"C:\Path\to\firefox";
IWebDriver driver = new FirefoxDriver(options)
根据:https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities
The name of the browser being used; should be one of {android, chrome, firefox, htmlunit, internet explorer, iPhone, iPad, opera, safari}
根据文档的内容,我假设您不需要设置“browserName”功能来使用自定义二进制文件。
关于java - 我无法在 Selenium 中将 Java 的解决方案重写为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47318176/