2

我有这个:

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.no_proxies_on", "localhost");
profile.setPreference("javascript.enabled", true);

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

FirefoxOptions options = new FirefoxOptions();
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);

现在我有两个不同的构造函数:

WebDriver driver = new FirefoxDriver(capabilities);

WebDriver driver = new FirefoxDriver(options);

我怎样才能将它们(功能和选项)都传递到driver?顺便说一句,IDE 告诉我它FirefoxDriver(capabilities)已被弃用。

4

2 回答 2

3

你快到了。您需要使用 MutableCapabilities 类中的方法merge()DesiredCapabilities类型的对象合并FirefoxOptions类型的对象中,并通过传递FirefoxOptions对象来启动WebDriverWebClient实例,如下所示:

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.no_proxies_on", "localhost");
profile.setPreference("javascript.enabled", true);

DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability("marionette", true);
capabilities.setCapability(FirefoxDriver.PROFILE, profile);

FirefoxOptions options = new FirefoxOptions();
options.merge(capabilities);
options.setLogLevel(Level.FINEST);
options.addPreference("browser.link.open_newwindow", 3);
options.addPreference("browser.link.open_newwindow.restriction", 0);

WebDriver driver = new FirefoxDriver(options);

参考

您可以在以下位置找到一些相关的讨论:

于 2019-08-14T13:13:21.677 回答
0

您可以将功能传递给firefoxoptions constructor 如下:

System.setProperty("webdriver.gecko.driver", "gecko/linux/geckodriver");

   FirefoxProfile profile = new FirefoxProfile();
   profile.setPreference("network.proxy.no_proxies_on", "localhost");
   profile.setPreference("javascript.enabled", true);

   DesiredCapabilities capabilities = DesiredCapabilities.firefox();
   capabilities.setCapability("marionette", true);

   FirefoxOptions options = new FirefoxOptions(capabilities);

set profile to firefox options
   options.setProfile(profile);
   options.setLogLevel(Level.FINEST);
   options.addPreference("browser.link.open_newwindow", 3);
   options.addPreference("browser.link.open_newwindow.restriction", 0);
pass firefox options as parameter to create driver
   WebDriver driver = new FirefoxDriver(options);
于 2019-12-10T15:21:46.320 回答