【问题标题】:How to pass the capabilities and options into Firefoxdriver using Selenium through Java如何通过 Java 使用 Selenium 将功能和选项传递给 Firefoxdriver
【发布时间】:2019-12-21 00:34:34
【问题描述】:

我有这个:

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) 已弃用。

【问题讨论】:

    标签: java selenium geckodriver desiredcapabilities mutablecapabilities


    【解决方案1】:

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

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

    参考文献

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

    【讨论】:

    • 它叫我java.lang.IllegalArgumentException: Preference browser.link.open_newwindow may not be overridden: frozen value=2, requested value=3...我该怎么办?
    • @assembler 该错误与您作为 browser.link.open_newwindow.restriction / 0 传递的 Key / Value 对有关。但这个答案是为了满足您将功能和选项传递给 Firefoxdriver 的需要
    【解决方案2】:

    您可以将功能传递给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);
    

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 2016-05-26
      • 2013-02-01
      • 2019-04-20
      • 2019-05-02
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多