创建Firefox配置文件并关闭牵线木偶

问题描述 投票:1回答:2

我来自Ruby背景,我知道如何在Ruby Selenium Binding中做到这一点,但我不知道怎么做Java Selenium Binding,

我有这个代码来创建Firefox配置文件

 FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
 WebDriver driver=new FirefoxDriver(firefoxProfile);

它适用于selenium 2.53但它在最近的selenium绑定3.11.0中引发了错误,有人能告诉我什么是替代方案吗?

而且我还想关掉牵线木偶以连接到Legacy Firefox驱动程序,我可以使用以下代码执行此操作

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", false);
WebDriver driver=new FirefoxDriver(capabilities);

但是,如果我使用上面的行,那么它就会弃用FirefoxDriver。任何人都可以指导我如何创建配置文件以及如何关闭牵线木偶?

java selenium selenium-webdriver
2个回答
5
投票

是的FirefoxDriver(desiredCapabilities)已被弃用。

替代方式是选择:

FirefoxOptions foptions =  new FirefoxOptions(capabilities);
WebDriver driver=new FirefoxDriver(foptions);  

更新:[按顺序]

FirefoxOptions foptions =  new FirefoxOptions();
FirefoxProfile firefoxProfile = new FirefoxProfile(pathToProfile);
foptions.setProfile(firefoxProfile);
foptions.setCapability("marionette", false);
foptions.setBinary("C:\\Program Files\\Mozilla Firefox 52\\firefox.exe"); 
WebDriver driver = new FirefoxDriver(foptions);

1
投票

要首先使用现有的Firefox配置文件进行测试执行,您必须按照Creating a new Firefox profile on Windows上的说明手动创建Firefox配置文件。现在,您必须将Firefox配置文件传递给FirefoxOptions类对象。此外,您将使用Legacy Firefox浏览器,您必须通过false类对象将marionatte设置为DesiredCapabilities,您需要将merge()放入FirefoxOptions类对象,如下所示:

System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
ProfilesIni profile = new ProfilesIni();
FirefoxProfile testprofile = profile.getProfile("debanjan");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(testprofile);
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability("marionatte", false);
options.merge(dc);
WebDriver driver = new FirefoxDriver(options);
driver.get("https://www.google.com");

更新

我不确定您的用例以及您希望使用Legacy Firefox Driver的原因。但根据GitHub的讨论,Unable to Start Firefox Using the Legacy Driver on a 3.5.3 Grid @jimevans明确提到:

旧版Firefox驱动程序不适用于Firefox 53左右。您可能会启动浏览器,但语言绑定将完全无法与驱动程序通信(因为Firefox将拒绝加载作为旧版Firefox驱动程序的浏览器扩展)。

@barancev还提到:

在“功能”块中,绑定不应通过符合W3C标准的有效负载部分的OSS功能。它们仅允许在“desiredCapabilities”块中。也许,Mozilla在发布频道中打破了Firefox 48中的Selenium兼容性,但在esr频道的第52版中恢复了它。这是出乎意料的,但这是真的。

这取决于你做出明智的决定。

© www.soinside.com 2019 - 2024. All rights reserved.