[英]java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap while using WebDriver with Maven Dependencies in Java Selenium
[英]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");
如果您使用maven删除 corrup/incompatible .m2
文件夹可以解决您的问题。
将JDK升级到最新级别JDK 8u222 。
Test
。tearDown(){}
方法中调用driver.quit()
以优雅地关闭和销毁WebDriver和Web Client实例。您可以在以下位置找到相关讨论:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.