【问题标题】:java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap error using GeckoDriver Firefox through Selenium in Javajava.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap 错误在 Java 中通过 Selenium 使用 GeckoDriver Firefox
【发布时间】:2020-04-06 17:35:16
【问题描述】:

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

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();
        }
    }

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

【问题讨论】:

    标签: java selenium guava noclassdeffounderror geckodriver


    【解决方案1】:

    此错误消息...

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

    ...暗示com/google/common/collect/ImmutableMap 文件可能已损坏,或者您使用的二进制文件版本与 guava version 之间存在一些不兼容 / 依赖(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重建你的项目只需要依赖。

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

    • 仅限 LinuxOSFree Up and Release the Unused/Cached Memory in Ubuntu/Linux Mint 在执行您的测试套件之前和之后。

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

    • 进行一次系统重启

    • 以非 root 用户身份执行您的 Test

    • 始终在 tearDown(){} 方法中调用 driver.quit() 以优雅地关闭和销毁 WebDriverWeb Client 实例。


    参考

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

    【讨论】:

    • 试过但还是同样的问题:(
    • 使用完整的错误堆栈跟踪更新问题
    • 完成请立即查看
    • @Gur 查看更新的答案并告诉我状态。
    • 我会试试这个并更新你...非常感谢
    猜你喜欢
    • 1970-01-01
    • 2022-10-07
    • 2018-10-14
    • 1970-01-01
    • 2020-09-16
    • 1970-01-01
    • 2020-09-12
    • 2018-01-18
    • 2018-05-26
    相关资源
    最近更新 更多