When I try launching new Selenium/Firefox instance with DesiredCapabilities and FirfoxOptions I get the following code:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;

I am using the following code:

public WebDriver getDriver() throws MalformedObjectNameException, InstanceNotFoundException, ReflectionException, InterruptedException
{

    System.setProperty("webdriver.gecko.driver", GlobalVar.geckdriverExecutableFilePath);

    //DesiredCapabilities capabilities = new DesiredCapabilities();



    DesiredCapabilities dc = DesiredCapabilities.firefox();


    if (proxyPOJO != null) {


        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setFtpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setSslProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());


        dc.setCapability(CapabilityType.PROXY, proxy);
    }

    FirefoxOptions opt = new FirefoxOptions();
    opt.merge(dc);


    opt.addPreference("dom.popup_maximum", 200);
    opt.addPreference("dom.webnotifications.enabled", false); 


    opt.merge(capabilities);


    WebDriver driver = WebDriverX.getNewFireFoxWebDriver(opt); // Basically calls: driver = new FirefoxDriver(firefoxOptions);  


    return driver;


}

My POM file contains the following entries:

<dependencies>

     <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.11.0</version>
    </dependency>

</dependencyManagement>

<dependencies>

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
</dependency>

 <dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>26.0-jre</version>
 </dependency>

Previously, I had version 3.5.2 of org.seleniumhq.selenium in POM which does not support merge functionality. However, when I tried launching Selenium with version 3.5.2 using following code:

System.setProperty("webdriver.gecko.driver", GlobalVar.geckdriverExecutableFilePath);
DesiredCapabilities capabilities = new DesiredCapabilities();

    if (proxyPOJO != null) {

        Proxy proxy = new Proxy();
        proxy.setHttpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setFtpProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());
        proxy.setSslProxy(proxyPOJO.getProxyIP() + ":" + proxyPOJO.getProxyPort());


        capabilities.setCapability(CapabilityType.PROXY, proxy);
    }
FirefoxOptions firefoxOptions = new FirefoxOptions(capabilities);
WebDriver driver = WebDriverX.getNewFireFoxWebDriver(firefoxOptions); 

I got the following exception:

NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.<init>(Lorg/openqa/selenium/Capabilities;)V

I have latest version of geckodriver.exe installed.

Neither version 3.11.0 or version 3.5.2 is working (I also tried 3.8.2).

What am I doing wrong?

Thanks!

UPDATE:

With version 3.11.0 I get the following stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;
    at webdriverX.WebDriverProfile.getTMPFirefoxProfile(WebDriverProfile.java:259)
    at s.SPage.scrapeS(SPage.java:36)
    at n.NMain.main(NMain.java:27)

With version 3.5.2 the following is the stack trace:

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.<init>(Lorg/openqa/selenium/Capabilities;)V
    at webdriverX.WebDriverProfile.getTMPFirefoxProfile(WebDriverProfile.java:232)
    at s.SPage.scrapeS(SPage.java:36)
    at n.NMain.main(NMain.java:27)

The method getTMPFirefoxProfile() basically does the following:

if (firefoxOptions != null) {
    driver = new FirefoxDriver(firefoxOptions);
} else {
                driver = new FirefoxDriver();
}

Thanks!!

1

There are 1 best solutions below

6
On

This error message...

Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;

...implies that there was no such method defined as merge() within the classes you have added.


merge()

merge() is defined in MutableCapabilities as:

public MutableCapabilities merge(Capabilities extraCapabilities)
    Merge the extra capabilities provided into this DesiredCapabilities instance. If capabilities with the same name exist in this instance, they will be overridden by the values from the extraCapabilities object.

Specified by:
    merge in interface Capabilities

Parameters:
    extraCapabilities - Additional capabilities to be added.

Returns:
    The DesiredCapabilities instance after the merge.

MutableCapabilities

MutableCapabilities was added in Selenium v3.7.0:

v3.7.0
======
* Migrated from using `DesiredCapabilities` to either
  `MutableCapabilities` or (preferably) `ImmutableCapabilities`.

Maven dependency

Further, you need to ensure you have added a single <dependency> for Selenium as follows:

<dependency>
  <groupId>org.seleniumhq.selenium</groupId>
  <artifactId>selenium-java</artifactId>
  <version>3.141.59</version>
</dependency>

Note: <dependency> for selenium-java implicitly also includes the <dependency> for guava.


Sample Code

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

Additional recommendations

Ensure that:

  • JDK is upgraded to current levels JDK 8u232.
  • Selenium is upgraded to current levels Version 3.141.59.
  • Upgrade GeckoDriver to GeckoDriver v0.26.0 level.
  • Upgrade Firefox version to Firefox v72.0 levels.
  • If your base Web Client version is too old, then uninstall it through Revo Uninstaller and install a recent GA and released version of Web Client.
  • Take a System Reboot.
  • Perform mvn clean, mvn build and mvn test
  • Execute your Test as a non-root user.

Note: In case of transitive dependency you may have to delete the MAVEN_HOME i.e. ~\.m2 sub directory.


Reference

You can find a couple of relevant discussion in: