Tuna
Tuna

Reputation: 171

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

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.

Upvotes: 17

Views: 72516

Answers (15)

yohong
yohong

Reputation: 1

i got this problem too when request google ads api. like: github issue

i update guava to the newest version . below:

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

and my google ads version:

        <dependency>
            <groupId>com.google.api-ads</groupId>
            <artifactId>google-ads</artifactId>
            <version>34.0.0</version>
        </dependency>

i hope this coude help u

Upvotes: 0

nativelectronic
nativelectronic

Reputation: 862

To ensure that the file is loaded correctly for Google APIs, you should first verify its existence and then confirm that it is being loaded as a stream. Google APIs require an InputStream, not a string or any other format, which could lead to exceptions if not handled properly. Here's a refined version of your message: Ensure File Existence and Correct Loading for Google APIs

Check File Existence: Before attempting to load the file, verify that it exists at the specified path. You can use the exists() method from the java.io.File class or the Files.exists() method from the java.nio.file package.
Load as InputStream: Make sure you are loading the file as an InputStream. Google APIs specifically require this format. 

---> Result: Loading it as a string or any other type may result in errors, such as those thrown by Guava. Guava cant read and thrown a exception if get bad format

///OK aproach

ClassPathResource resourceCredential = new ClassPathResource("ga4credentials/client_secrets.json");

        InputStream inputStream = resourceCredential.getInputStream();
        GoogleCredentials credentials = GoogleCredentials.fromStream(inputStream)

/// DO NOT

ClassPathResource resourceCredential = new ClassPathResource("client_secrets.json");
       String stringPathCredential = new String(Files.readAllBytes(resourceCredential.getFile().toPath()));

Upvotes: 0

Naveen Dwivedi
Naveen Dwivedi

Reputation: 1

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

Upvotes: 0

Dmitri Algazin
Dmitri Algazin

Reputation: 3456

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>

Upvotes: 0

Venkatesh Thatha
Venkatesh Thatha

Reputation: 21

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

Upvotes: 2

Enzo33B3I1I2
Enzo33B3I1I2

Reputation: 1

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>

Upvotes: 0

PVDM
PVDM

Reputation: 118

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

Upvotes: 3

Thư Nguyễn
Thư Nguyễn

Reputation: 41

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>

Upvotes: 3

siva
siva

Reputation: 11

use version version 4.1.1

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

Upvotes: 1

Asraful Haque
Asraful Haque

Reputation: 1125

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>

Upvotes: 1

Upasana
Upasana

Reputation: 91

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>

Upvotes: 9

rieckpil
rieckpil

Reputation: 12021

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.

Upvotes: 19

Alex Boghda
Alex Boghda

Reputation: 141

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>

Upvotes: 14

Alex.K
Alex.K

Reputation: 294

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.

Upvotes: 2

undetected Selenium
undetected Selenium

Reputation: 193088

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.

Upvotes: 2

Related Questions