Home > front end >  java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap error when trying to execu
java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap error when trying to execu

Time:02-14

I am new to selenium tests and I am currently watching some youtube videos using maven.

Today I tried a few codes and worked fine but when accessing one store page and trying to search a product it gives me "Access Denied" message So I tried a few ways to open chrome instead of chromedriver.

But I gave up and was willing to try another webpage. But now chromedriver wont open (gives error message). operadriver, geckodriver and etc will immediately close after launch.

I am getting this error:

java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap com.google.common.collect.ImmutableMap.of(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)'

I tried looking in a lot of places but most said it was guava version, I dont use guava, even though I tried adding it to dependencies...

My code is this:

public class TestYoutubeClass {
    WebDriver driver;
    @Before
    public void runDriver(){
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.kabum.com.br");

I tried starting from the beginning and nothing worked. I am using IntelliJ On pom.xml I added: junit; selenium-java and webdrivermanager.

Every help is really appreciated.

CodePudding user response:

This error message...

java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap com.google.common.collect.ImmutableMap.of(java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object, java.lang.Object)'

...implies that there is NoSuchMethod as com.google.common.collect.ImmutableMap which indicates it might be corrupted or there is some incompatibility between the version of the binaries you are using specifically with the guava version / dependency (maven).


You need to take care of a couple of things as follows:

  • You need to add the System.setProperty():

    System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Java\\jre1.8.0_231\\lib\\ext\\geckodriver.exe");
    
  • Use only a single instance of WebDriver, either from the class scope or from the method scope.

  • Effectively, you line of code will be;

    public class TestYoutubeClass {
        WebDriver driver;
        @Before
        public void runDriver(){
            System.setProperty("webdriver.chrome.driver","/path/to/chromedriver");
            driver = new ChromeDriver();
            driver.get("http://www.kabum.com.br");
    
  • Incase you are using deleting the corrupt/incompatible .m2 folder can solve your issue.

CodePudding user response:

Theres seems to be a bug in 5.0.x versions, see last two messages in this thread - https://github.com/bonigarcia/webdrivermanager/issues/576

Try switching the webdrivermanager dependency version to 4.4.3, worked for me.

  • Related