java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap error when trying to execute tests using Chromedriver and Maven
Asked Answered
S

13

17

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.

Spreader answered 12/2, 2022 at 20:20 Comment(0)
R
17

The issue comes from conflicting Guava (transitive dependency) versions.

It should be fixed with the WebDriverManager version 5.1.0.

As an alternative, you can also use the <dependencyManagement> section of your pom.xml to force a resolution of the latest Guava version:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>31.0.1-jre</version>
    </dependency>
  </dependencies>
</dependencyManagement>

or exclude the Guava dependency from the WebDriverManager (no longer required for >= 5.1.0):

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.0.3</version>
    <exclusions>
        <exclusion>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
        </exclusion>
    </exclusions>
</dependency>

PS: To ensure dependency convergence for your Maven project, consider adding the Maven Enforcer Plugin to detect multiple versions of the transitive dependency early on.

Radbourne answered 18/2, 2022 at 13:22 Comment(3)
Only adding to the <dependencyManagement> section solved the issue for meTarentarentum
I used the second method you posted, adding the block to the same pom.xml that contains the org.selenium.hq dependencies. Rebuilt and did a clean maven install. No change. Still fails with java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMapGossipmonger
I suggest adding <scope>test</scope> to the dependency.Bradfordbradlee
S
14

I'm using maven and downgraded Version 4.1.2 to 4.1.1 solved my problem

<dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>4.1.1</version>
</dependency>
Shani answered 17/2, 2022 at 2:48 Comment(3)
What version of webdrivermanager are you using?Garrek
ma man !!!!!!!!!Backflow
this version is very old now. which version should we use today ?Leucopenia
K
9

I also faced the above issue after adding below dependency it worked fine. I am using Chrome 98 version

<dependencies>
    <dependency>
      <groupId>com.google.guava</groupId>
      <artifactId>guava</artifactId>
      <version>31.0.1-jre</version>
    </dependency>
  </dependencies>
Konrad answered 24/2, 2022 at 12:9 Comment(1)
updating the guava version helped me resolve the issue! thanksClastic
T
3

Just add

    workbook.close();

Or try this code in your pom.xml, it works for me

    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.0.1-jre</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.2.0</version>
    </dependency>

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.0.1</version>
    </dependency>
Tubule answered 15/6, 2022 at 7:44 Comment(0)
D
3

Looking at the master branch of Guava today, it looks like the actual issue is simply that no version of com.google.common.collect.ImmutableMap.of(...) accepts more than 10 parameters (the thread starter's error shows 12). It is documented in the source code here:

https://github.com/google/guava/blob/master/guava/src/com/google/common/collect/ImmutableMap.java

This is the comment after the version that accepts 10 entries: // looking for of() with > 10 entries? Use the builder or ofEntries instead.

I am suggesting the usage of .of() in Selenium is changed to on of the suggested methods in the copied comment above since this seems to be a recurring issue for a lot of people.

Will try to re-open this issue to get this taken care of: https://github.com/SeleniumHQ/selenium/issues/10324

Depose answered 9/7, 2022 at 19:37 Comment(0)
S
2

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.

Shipowner answered 12/2, 2022 at 20:50 Comment(0)
C
2

Theres seems to be a bug in 5.0.x versions of webdrivermanager, 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.

Cullie answered 14/2, 2022 at 12:6 Comment(1)
there's been an update to webdrivermanager, looks like this issue is fixed in version 5.1.0 now.Cullie
T
2
<dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>31.0.1-jre</version>
    </dependency>
<!-- https://mvnrepository.com/artifact/io.github.bonigarcia/webdrivermanager  -->
<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.1.0</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>4.2.0</version>
</dependency>
I used above version in pom.xml.This will help me to come out of this error.
Trademark answered 5/1, 2023 at 14:21 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Samuella
N
1

If you are using Maven pom.xml file then check the version of the WebDriverManager below 5.1.0 . I used the below version

(5.1.0)

to solve the shown issue!

<dependency>
    <groupId>io.github.bonigarcia</groupId>
    <artifactId>webdrivermanager</artifactId>
    <version>5.1.0</version>
</dependency>
Newly answered 9/5, 2022 at 9:2 Comment(0)
C
1

use version version 4.1.1

<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.1</version>
    </dependency>
Crispa answered 14/6, 2022 at 20:12 Comment(0)
T
0

I have personally solved these with these versions:

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.boot.version>1.5.21.RELEASE</spring.boot.version>
</properties>

<dependencies>

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

    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>5.0.1</version>
    </dependency>

</dependencies>
Tundra answered 17/11, 2022 at 13:16 Comment(2)
What solved it? You're posting a bunch of versions, but not a solution?Gossipmonger
To solved this error (java.lang.NoSuchMethodError: 'com.google.common.collect.ImmutableMap error when trying to execute tests using Chromedriver and Maven) I have change the versions of my dependencies. With these versions I no longer have the above errorTundra
D
0

Spend another 3 hours fighting with selenium.

That worked for me:

    <selenium.version>4.1.2</selenium.version>
    <selenium-webdrivermanager.version>5.0.1</selenium-webdrivermanager.version>
    <guava.version>31.0.1-jre</guava.version>


    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-leg-rc</artifactId>
        <version>${selenium.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.testng</groupId>
                <artifactId>testng</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-chrome-driver</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-remote-driver</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-support</artifactId>
        <version>${selenium.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.bonigarcia</groupId>
        <artifactId>webdrivermanager</artifactId>
        <version>${selenium-webdrivermanager.version}</version>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>
Dourine answered 5/1, 2023 at 18:6 Comment(1)
<guava.version>31.0.1-jre</guava.version> is the crucial part.Bradfordbradlee
B
0

It solved while I am using this dependency. Use selenium 4.1.1 version.

Bulla answered 23/10, 2023 at 9:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Samuella

© 2022 - 2024 — McMap. All rights reserved.