java - WebDriver 和 ChromeDriver:无法修复 org/json/JSONException 错误
问题描述
我是使用 Selenium WebDriver 的新手。我正在尝试使用 ChromeDriver,以下是我的代码:
public class WebScraper {
public WebScraper() {
}
public WebDriver driver = new ChromeDriver();
public void openTestSite() {
driver.navigate().to("https://google.com");
}
public void enter(String word) {
WebElement query_editbox =
driver.findElement(By.id("query"));
query_editbox.sendKeys(word);
query_editbox.sendKeys(Keys.RETURN);
}
// public void getText() {
// //List<WebElement> searchResults =
// driver.findElements(By.xpath("//div[@id='mid']/div"));
// // Writer writer = new BufferedWriter(new
// OutputStreamWriter(new FileOutputStream("status.txt"),
// "utf-8"));
// //int[] index = {0};
//
// WebElement result=driver.findElement(By.id("mid"));
// System.out.println(result.getText());
//}
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.chrome.driver", "chromedriver");
System.out.println("Hello");
WebScraper webScraper = new WebScraper();
webSrcapper.openTestSite();
webSrcapper.enter("avoir");
//webSrcapper.getText();
System.out.println("Hello");
}
}
我收到以下错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
at org.openqa.selenium.remote.RemoteWebDriver.init(RemoteWebDriver.java:110)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:92)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:144)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:86)
at WebScraper.<init>(WebScraper.java:15)
at WebScraper.main(WebScraper.java:47)
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
有人可以帮我解决这个问题并告诉我为什么我首先收到这个错误吗?
谢谢!
解决方案
错误
线程“主”java.lang.NoClassDefFoundError 中的异常:org/json/JSONException
是因为它缺少依赖org.json
添加这个https://mvnrepository.com/artifact/org.json/json/20180813
推荐阅读
- java - 如何获取旧的 MS-office 文件的最后修改日期,例如 .xls 和 .doc 文件
- vue.js - error 解析错误:意外的令牌(Nuxt.js)
- ajax - How to refresh table data with Ajax
- validation - 如何根据范围为输入数字制定规则?
- python - 为什么在openCV灰度处理之后,通道消失了?
- php - 如何使用 PHP 从用户定义的值增加 mysql 数据库列中的值?
- bitrock - 如何本地化内置 BitRock 变量
- python - 当 sizer 项目隐藏时,GridBagSizer 可以拉伸吗?
- java - 如何在 Android 中通信两个服务?
- java - 扩展gridview时,为什么需要在构造函数中调用super?