5

I am trying to use Selenium api with Gradle. This is my build.gradle dependency section:

dependencies { compile 'com.google.api-client:google-api-client:1.23.0' compile 'com.google.oauth-client:google-oauth-client-jetty:1.23.0' compile 'com.google.apis:google-api-services-sheets:v4-rev506-1.23.0' compile group: 'org.seleniumhq.selenium', name: 'selenium-java', version: '2.9.0' compile group: 'org.seleniumhq.selenium', name: 'selenium-chrome-driver', version: '2.9.0' } 

My selenium - Java code:

System.setProperty("webdriver.chrome.driver", "C:\\Program Files(x86)\\Google\\Chrome\\Application\\chrome.exe"); WebDriver driver = new ChromeDriver(); 

Code works fine, and I am able to get Chrome browser opened. However, in build.gradle, I am using 2.9.0 version of selenium and chromedriver. If I try to use any version after 2.9.0, it gives me below error in WebDriver driver = new ChromeDriver(); method:

Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124) at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:32) at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:137) at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:339) at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:88) at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123) at Quickstart.main(Quickstart.java:130) 

I tried looking for gradle+Maven+selenium supported version. Was not able to find any good info. Any idea?

    9 Answers 9

    4

    Try to update your Guava to

    <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency> 

    It will solve your issue.

      4

      This error message...

      Exception in thread "main" java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)V 

      ...implies that the Java Client was unable to find ChromeDriver().

      Issue & Solution

      As per the Selenium - Java code you have shared, the System.setProperty() line is used to set the ChromeDriver binary path not the chrome binary path. For that you have to download the ChromeDriver binary from the ChromeDriver - WebDriver for Chrome and place it in your system and mention the absolute path of the ChromeDriver through System.setProperty() line. Hence you have to change :

      System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"); WebDriver driver = new ChromeDriver(); 

      To :

      System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); 
      4
      • Thanks for your reply! I tried downloading chromedriver.exe and using it instead of chrome.exe, problem still remains the same. Question is why everything works fine (irrespective of using chrome.exe or chromedriver.exe) when build.gradle has selenium dependency <= 2.9.0. And it is failing to open a browser for selenium dependencies > 2.9.0CommentedFeb 28, 2018 at 19:51
      • @SwatiThorve As per current release of Selenium Client v3.9.1selenium dependencies > 2.9.0 are pretty much ancient. A lot have changed since Selenium v2.9.0 days. So as per best practices you need to upgrade Selenium Client, ChromeDriver and Chrome to current level for better script performance.CommentedMar 1, 2018 at 8:23
      • Thanks Debanjan! Yes, I am aware 2.9.0 is pretty ancient...I too don't want to use it. Thats why the question :) why I am getting error described in my original post when I try to use selenium 2.9+? I have latest chrome V67 and chromedriver. Anything else I need to check?CommentedMar 1, 2018 at 9:46
      • Pick up all the latest releases in terms of JDK v8u162, Selenium v3.9.1, ChromeDriver v2.35 and Chrome v64.x. You will be through.CommentedMar 1, 2018 at 9:56
      1

      I have the exact same problem (I am using Maven though).

      I noticed that the problem is that using one of com.google.api-client, or com.google.oauth-client, or com.google.apis:google-api-services-sheets alongside org.seleniumhq.selenium causes the error.

      The problem is that both dependencies depend on a different com.google.guava:guava artifact.

      In order to solve the error, you should explicitly depend on a single com.google.guava:guava artifact. So go ahead and add the following in your build.gradle:

      compile 'com.google.guava:guava:27.0.1-jre'

        0

        Just wanted to post here in case anyone else comes to this from Google like I did. For whatever reason, I needed to run with sudo. I was having issues using the npm selenium-standalone package and running:

        /node_modules/selenium-standalone/bin/selenium-standalone start 

        And it would show that error. What fixed it was running with sudo

        sudo /node_modules/selenium-standalone/bin/selenium-standalone start 

        I don't think I needed to do this before but suddenly it's the only way it works now.

          0

          I had the same problem and ran a dependency check and found that there were conflicts. The solution that worked for me was to exclude the conflicting dependencies.

          Your project will probably have different dependencies than mine. So, listing the specific conflicts in my project may not be helpful.

            0

            copy and paste the following dependencies in the pom.xml and then do a maven build:

             <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.testng/testng --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.1.0</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>rest-assured</artifactId> <version>3.0.0</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.rest-assured/json-path --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-path</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.rest-assured/json-schema-validator --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>json-schema-validator</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/io.rest-assured/xml-path --> <dependency> <groupId>io.rest-assured</groupId> <artifactId>xml-path</artifactId> <version>3.0.0</version> </dependency> <!-- https://mvnrepository.com/artifact/org.hamcrest/java-hamcrest --> <dependency> <groupId>org.hamcrest</groupId> <artifactId>java-hamcrest</artifactId> <version>2.0.0.0</version> <scope>test</scope> </dependency> <!-- cucumber dependency begins --> <!-- https://mvnrepository.com/artifact/net.masterthought/cucumber-reporting --> <dependency> <groupId>net.masterthought</groupId> <artifactId>cucumber-reporting</artifactId> <version>4.7.0</version> </dependency> <!-- starts here --> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-core</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-java</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm</artifactId> <version>1.2.5</version> <type>pom</type> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-junit</artifactId> <version>1.2.5</version> <scope>test</scope> </dependency> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-jvm-deps</artifactId> <version>1.0.5</version> </dependency> <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-html --> <dependency> <groupId>info.cukes</groupId> <artifactId>cucumber-html</artifactId> <version>0.2.3</version> </dependency> <!-- https://mvnrepository.com/artifact/info.cukes/gherkin --> <dependency> <groupId>info.cukes</groupId> <artifactId>gherkin</artifactId> <version>2.12.2</version> <scope>provided</scope> </dependency> <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng --> <dependency> <groupId>io.cucumber</groupId> <artifactId>cucumber-testng</artifactId> <version>5.4.2</version> </dependency> <!-- https://mvnrepository.com/artifact/com.theoryinpractise/cucumber-testng-factory --> <dependency> <groupId>com.theoryinpractise</groupId> <artifactId>cucumber-testng-factory</artifactId> <version>1.0.1</version> </dependency> <!-- https://stackoverflow.com/questions/49021707/java-lang-nosuchmethoderror-com-google-common-base-preconditions-checkstatezlj?rq=1 --> <!-- https://mvnrepository.com/artifact/com.google.guava/guava --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>27.1-jre</version> </dependency> </dependencies> 
            1
            • 1
              It would be nice to give further information on why and how your solution works/what it does.
              – ich5003
              CommentedMar 12, 2020 at 22:10
            0

            Just adding the below was not enough

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

            It was not working first. Then I moved this dependency up higher in pom.xml than junit dependency and it worked. So, make sure that in pom file ,it is higher than junit or testng or whatever runner you are using

            A comment in this post helped

              0

              Adding guava dependency and chromedriver version dependency worked for me -

              <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>25.0-jre</version> </dependency> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-chrome-driver</artifactId> <version>3.141.59</version> <scope>test</scope> </dependency> 
                0

                Problem might be also in inclusion of the google-collections:

                // https://mvnrepository.com/artifact/com.google.collections/google-collections implementation 'com.google.collections:google-collections:1.0' 

                I had this included in one library that I linnked in and it drived me nuts finding the reason. Including parameter -verbose:class into JVM helped to pinpoint the culprit.

                  Start asking to get answers

                  Find the answer to your question by asking.

                  Ask question

                  Explore related questions

                  See similar questions with these tags.