简体   繁体   中英

WebDriver and ChromeDriver: Unable to fix org/json/JSONException error

I am new to using Selenium WebDriver. I am trying to use ChromeDriver, and the following is my code:

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");

}
}

I am receiving the following error:

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

Can someone please help me resolve this and tell me why I am receiving this error in the first place?

Thanks!

The error

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException

is because it missing dependency org.json add this https://mvnrepository.com/artifact/org.json/json/20180813

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM