簡體   English   中英

java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap 在 Java 中通過 Selenium 使用 GeckoDriver Firefox 時出錯

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

我正在使用此代碼,它給了我這個錯誤:

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可能已損壞,或者您正在使用的二進制文件版本番石榴版本/依賴項(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");
  • 如果您使用刪除 corrup/incompatible .m2文件夾可以解決您的問題。

  • JDK升級到最新級別JDK 8u222

  • Selenium升級到當前級別版本 3.141.59
  • 升級GeckoDriverGeckoDriver v0.26.0水平。
  • GeckoDriver 存在於所需位置。
  • GeckoDriver 對非 root 用戶具有可執行權限。
  • Firefox版本升級到Firefox v70.0級別。
  • 通過IDE清理項目工作區並僅使用所需的依賴項重建項目。
  • 僅限Windows 操作系統)在執行測試套件之前和之后,使用CCleaner工具清除所有操作系統雜務。
  • 僅限 LinuxOS )在執行測試套件之前和之后釋放和釋放 Ubuntu/Linux Mint 中未使用/緩存的內存
  • 如果您的基本Web Client版本太舊,請通過Revo Uninstaller卸載它並安裝最新的 GA 和發布版本的Web Client
  • 進行系統重啟
  • 以非 root 用戶身份執行您的Test
  • 始終在tearDown(){}方法中調用driver.quit()以優雅地關閉和銷毀WebDriverWeb Client實例。

參考

您可以在以下位置找到相關討論:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM