-1

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!!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Alan Cook
  • 342
  • 3
  • 14
  • `NoSuchMethodError`indicates incompatible jar files. Add the complete stacktrace – Jens Feb 05 '20 at 06:45
  • Update the question with the complete error stack trace – undetected Selenium Feb 05 '20 at 07:16
  • desired capabilities are used mainly with remote web driver. are you running on selenium grid? – Murthi Feb 05 '20 at 07:57
  • @Jens @DebanjanB @Murthi I updated the questions with `stack trace` and additional details. Please take a look. Thank you all for your help.. – Alan Cook Feb 05 '20 at 16:23
  • @Murthi No I'm not but I need desired capabilities to add proxy: `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);` And I need `FirefoxOptions` to set values such as `opt.addPreference("dom.popup_maximum", 200); opt.addPreference("dom.webnotifications.enabled", false);` – Alan Cook Feb 05 '20 at 16:26
  • Why are you not using the latest [Selenium](https://search.maven.org/artifact/org.seleniumhq.selenium/selenium-java) release version? What is the Firefox version? – Boris Feb 05 '20 at 16:27
  • @Boris `4.0.0-alpha-4`? I tried it I get `Exception in thread "main" java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.(Lorg/openqa/selenium/Capabilities;)V` – Alan Cook Feb 05 '20 at 16:31
  • @Boris I'm running `Version 72.0.2 (64-bit)` – Alan Cook Feb 05 '20 at 17:00

1 Answers1

-1

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:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • @AlanCook Delete the `maven_home` directory `.m2` and rebuild. – undetected Selenium Feb 05 '20 at 17:41
  • I ran `Maven clean` then `Maven build` ( I assume this is what you were referring to) but the same exception persists.. – Alan Cook Feb 05 '20 at 17:45
  • @AlanCook Precisely that was what I meant, yet at times the incompatible dependencies creates choas as I suspect in your case with Guava. Hence I wish you delete the `MAVEN_HOME` – undetected Selenium Feb 05 '20 at 18:04
  • OK, I created a brand new project to make sure there are no unknown conflicting dependencies.. followed your advice and `WebDriver` launches as expected.. I'm a little lost here.. does this mean perhaps one of the projects my current project depends on relies on earlier (incompatible) version of `Selenium`? I know that one project relies on `AShot` 1.5.2 which in turn relies on `Selenium version 2.4.7` -- however I do not see version `2.4.7` in any build path or effective `POM`.. How can I confirm that this is the case? – Alan Cook Feb 05 '20 at 18:30
  • @AlanCook _...WebDriver launches as expected..._ Glad to be able to help you. Yes, there seems to be cross dependencies between multiple version of _Selenium_ and _Guava_. Possibly some/any of the existing set of binaries got corrupted. – undetected Selenium Feb 05 '20 at 20:44
  • @Debanjan_B No it did not solve the problem. Like I said, WebDriver launches as expected when using exact same code in a different project. In the project in question I'm still getting the same `java.lang.NoSuchMethodError: org.openqa.selenium.firefox.FirefoxOptions.merge(Lorg/openqa/selenium/Capabilities;)Lorg/openqa/selenium/firefox/FirefoxOptions;` and I have no idea how to debug it or check if in fact the error stems from conflicting dependencies.. Kindly advise.. – Alan Cook Feb 06 '20 at 03:07