如何通过Java使用Selenium将功能和选项传递给Firefoxdriver [英] How to pass the capabilities and options into Firefoxdriver using Selenium through Java
问题描述
我有这个:
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.
推荐答案
您快到了.您需要使用方法 MutableCapabilities 类,将对象的 DesiredCapabilities 类型合并到 FirefoxOptions 键入对象,并通过如下所示传递 FirefoxOptions 对象来启动 WebDriver 和 WebClient 实例:
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:
- 如何要使用xvfb将具有所需功能的Chrome驱动程序服务合并到无头的位置?
- 如何解决构造函数ChromeDriver(Capabilities)已弃用"和WebDriverException:ChromeDriver和Chrome超时错误
- 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屋!