How to pass the capabilities and options into Firefoxdriver using Selenium through Java

亡梦爱人 提交于 2020-01-05 03:46:31

问题


I have this:

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

and

WebDriver driver = new FirefoxDriver(options);

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.


回答1:


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



回答2:


you can pass capabilities into firefoxoptions constructor as below :

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


来源:https://stackoverflow.com/questions/57494915/how-to-pass-the-capabilities-and-options-into-firefoxdriver-using-selenium-throu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!