java - java.lang.NoClassDefFoundError: com/google/common/collect/ImmutableMap 错误在 Java 中通过 Selenium 使用 GeckoDriver Firefox
问题描述
我正在使用这段代码,它给了我这个错误:
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。
将Selenium升级到当前级别版本 3.141.59。
将GeckoDriver升级到GeckoDriver v0.26.0级别。
GeckoDriver 出现在所需位置。
GeckoDriver 对非 root 用户具有可执行权限。
将Firefox版本升级到Firefox v70.0级别。
通过IDE清理项目工作区并仅使用所需的依赖项重建项目。
(仅限Windows 操作系统)使用CCleaner工具在执行测试套件之前和之后清除所有操作系统琐事。
(仅限 LinuxOS)在执行测试套件之前和之后释放和释放 Ubuntu/Linux Mint 中未使用的/缓存的内存。
如果您的基本Web Client版本太旧,请通过Revo Uninstaller卸载它并安装最新的 GA 和已发布版本的Web Client。
重新启动系统。
Test
以非 root 用户身份执行。始终
driver.quit()
在tearDown(){}
方法内调用以优雅地关闭和销毁WebDriver和Web 客户端实例。
参考
您可以在以下位置找到相关讨论:
推荐阅读
- algorithm - 将十进制转换为二进制并将其用作过滤器
- c++ - C ++:无法在编译时使用模板函数评估完美转发的integral_constant
- sql - MariaBD 替代模式
- java - Jackson 从简单的 String 序列化 Joda DateTime
- unit-testing - 在单元测试中触发 vue-select 下拉项选择
- python - 为什么训练这么慢?
- postgresql - PostgreSQL 不同步时如何重置所有表的主键序列?
- python - Keras 内置的 2D 数据 MSE 损失返回 2D 矩阵,而不是标量损失
- javascript - 从数组中删除项目:删除了错误的项目
- javascript - 在 Windows 10 中使用 NodeJS 在本地运行异步函数