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

后端 未结 2 1511
余生分开走
余生分开走 2021-01-27 01:51

I have this:

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

FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(\         


        
2条回答
  •  清酒与你
    2021-01-27 02:19

    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

提交回复
热议问题