java.lang.NoClassDefFoundError : com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Java

标签 java selenium guava noclassdeffounderror geckodriver

我正在使用这段代码,它给了我这个错误:

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
        at org.openqa.selenium.firefox.FirefoxDriver.<clinit>FirefoxDriver.java:108)
        at Selenium_1.main(Selenium_1.java:13)
    Caused by: java.lang.ClassNotFoundException: com.google.common.collect.ImmutableMap
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 2 more

无法解决。我正在 eclipse 中工作,你能帮我一下吗?

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//comment the above line and uncomment below line to use Chrome
//import org.openqa.selenium.chrome.ChromeDriver;

public class Selenium_1 {
        public static void main(String[] args) {
            // declaration and instantiation of objects/variables
            System.setProperty("webdriver.firefox.marionette","C:\\Program Files\\Java\\jre1.8.0_231\\lib\\ext\\geckodriver.exe");
            //System.setProperty("webdriver.chrome.driver", "/path/to/chrome driver");
            WebDriver driver = new FirefoxDriver();
            //comment the above 2 lines and uncomment below 2 lines to use Chrome
            //System.setProperty("webdriver.chrome.driver","G:\\chromedriver.exe");
            //WebDriver driver = new ChromeDriver();

            String baseUrl = "http://demo.guru99.com/test/newtours/";
            String expectedTitle = "Welcome: Mercury Tours";
            String actualTitle = "";

            // launch Fire fox and direct it to the Base URL
            driver.get(baseUrl);

            // get the actual value of the title
            actualTitle = driver.getTitle(); 

            /*
             * compare the actual title of the page with the expected one and print
             * the result as "Passed" or "Failed"
             */
            if (actualTitle.contentEquals(expectedTitle)){
                System.out.println("Test Passed!");
            } else {
                System.out.println("Test Failed");
            }



            //close Fire fox
            driver.close();
        }
    }

如果您需要了解其他信息,请告诉我...我完全陷入困境...帮助!帮助!帮助!

最佳答案

此错误消息...

Exception in thread "main" java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap
    at org.openqa.selenium.firefox.FirefoxDriver

...暗示文件 com/google/common/collect/ImmutableMap 可能已损坏,或者您专门与 guava 使用的二进制文件版本之间存在一些不兼容 版本/依赖 (maven)。

<小时/>

您需要注意以下几件事:

  • System.setProperty() 行中,您需要将 webdriver.firefox.marionette 更改为 webdriver.gecko.driver。因此,有效地,该行代码将是:

    System.setProperty("webdriver.gecko.driver","C:\\Program Files\\Java\\jre1.8.0_231\\lib\\ext\\geckodriver.exe");
    
  • 如果您使用 删除损坏/不兼容的 .m2 文件夹可以解决您的问题。

  • JDK 升级到最新级别 JDK 8u222 .

  • Selenium升级到当前级别Version 3.141.59 .

  • 将 GeckoDriver 升级至 GeckoDriver v0.26.0级别。

  • GeckoDriver 位于所需位置。

  • GeckoDriver 对非 root 用户具有可执行权限。

  • Firefox 版本升级至 Firefox v70.0 级别。

  • 通过IDE清理您的项目工作区,并仅使用所需的依赖项重建项目。

  • (仅限 WindowsOS)使用 CCleaner工具可以清除执行测试套件之前和之后的所有操作系统杂务。

  • (仅限 LinuxOS)Free Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint执行测试套件之前和之后。

  • 如果您的基本Web客户端版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和已发布版本的Web 客户端

  • 重新启动系统

  • 以非 root 用户身份执行测试

  • 始终在 tearDown(){} 方法中调用 driver.quit() 来关闭并销毁 WebDriver>Web 客户端 优雅地实例化。

<小时/>

引用

您可以在以下位置找到相关讨论:

关于java.lang.NoClassDefFoundError : com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59319629/

相关文章:

java - 在java中将递归帕斯卡三角形居中?

java - 为什么 Selenium 驱动程序无法识别 Facebook 登录页面的 ID 元素?

javascript - 如何使用selenium Webdriver获取gmail的密码输入字段ID?

python - 我在解析网页中的表格时遇到问题

java - readResolve 不起作用? : an instance of Guava's SerializedForm appears

java - Guava LoadingCache getAll - 但没有任何参数?

java - RabbitMQ:多消息和单一消费者

java - 使用套接字立即从服务器向客户端发送数据时出现问题吗?

java - 为什么我会出现 0 的无限循环? ( java )

java - Guava Cache - 按身份比较值的含义是什么