如何通过Java使用Selenium将功能和选项传递给Firefoxdriver [英] How to pass the capabilities and options into Firefoxdriver using Selenium through Java

查看:243
本文介绍了如何通过Java使用Selenium将功能和选项传递给Firefoxdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

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);

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

Now I have two different constructors:

WebDriver driver = new FirefoxDriver(capabilities);

WebDriver driver = new FirefoxDriver(options);

如何将它们(功能和选项)都传递给driver?顺便说一下,IDE告诉我FirefoxDriver(capabilities)已过时.

How can I pass them both (capabilities and options) into the driver? By the way, the IDE is telling me that FirefoxDriver(capabilities) is deprecated.

推荐答案

您快到了.您需要使用方法

You were almost there. You need to use the method merge() from MutableCapabilities Class to merge the DesiredCapabilities type of object into FirefoxOptions type object and initiate the WebDriver and WebClient instance by passing the FirefoxOptions object as follows :

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);


参考文献

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


References

You can find a couple of relevant discussions in:

  • How to Merge Chrome driver service with desired capabilities for headless using xvfb?
  • How to address "The constructor ChromeDriver(Capabilities) is deprecated" and WebDriverException: Timed out error with ChromeDriver and Chrome

这篇关于如何通过Java使用Selenium将功能和选项传递给Firefoxdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆