2

i am trying to fetch data from a website using Selenium automation when i am trying to access data from that web site i am getting following exception

run:
Starting ChromeDriver (v2.9.248315) on port 15621
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/entity/ContentType
    at org.openqa.selenium.remote.HttpCommandExecutor$EntityWithEncoding.<init>(HttpCommandExecutor.java:411)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:306)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:66)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:568)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:240)
    at org.openqa.selenium.chrome.ChromeDriver.startSession(ChromeDriver.java:181)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:126)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:139)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:160)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:149)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:106)
    at pocmandi.PocMandi.main(PocMandi.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.http.entity.ContentType
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 12 more

Here is my cod

package pocmandi;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import java.util.StringTokenizer;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import org.openqa.selenium.support.ui.Select;
import java.sql.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class PocMandi {

    Statement st = null;
    Connection cn = null;

    public static void main(String args[]) throws InterruptedException, ClassNotFoundException, SQLException {

        int j = 0;
        String htmlTableText = null;
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\SHAKTI\\Desktop\\JarFiles\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        String commodity = "Jo";
        String commo[] = {"Paddy", "Rice", "Jwar", "Barley", "Corn", "Wheat", "Jo", "Bejhar", "Jai", "Urad", "Moong", "Chana", "Matar"};
        for (String com : commo) {
            String sDate = "27/03/2014";
            String url = "http://www.upmandiparishad.in/commodityWiseAll.aspx";
            driver.get(url);
            Thread.sleep(5000);

            new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText(com);
            driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys(sDate);

            Thread.sleep(3000);
            driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
            Thread.sleep(5000);

            WebElement findElement = driver.findElement(By.id("ctl00_ContentPlaceHolder1_GridView1"));
            // WebElement find=driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"));
            htmlTableText = findElement.getText();
      //  String html=find.getText();
            // do whatever you want now, This is raw table values.
            htmlTableText = htmlTableText.replace("S.No.DistrictMarketPrice", "");
            htmlTableText = htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1");
            htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", "");
            htmlTableText = htmlTableText.replace("S.No. District Market Price", "");
            System.out.println(htmlTableText);

            String s[] = htmlTableText.split("");
            StringTokenizer str = new StringTokenizer(htmlTableText);
            while (str.hasMoreTokens()) // for(int i=0;i<s.length;i++)
            // if(str.hasMoreElements())
            {
                String no = str.nextElement().toString();

                String city = str.nextElement().toString();
                String mandi = str.nextElement().toString();
                String price = str.nextElement().toString();
                Class.forName("com.mysql.jdbc.Driver");
                Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mandi", "root", "");
                //insert them into the database
                PreparedStatement ps = cn.prepareStatement("insert into commoditydemo values(?,?,?,?,?,?)");
                ps.setString(1, no);
                ps.setString(2, city);
                ps.setString(3, mandi);
                ps.setString(4, price);
                ps.setString(5, com);
                ps.setString(6, "0");
                j = ps.executeUpdate();
                cn.close();

            }
        }
        driver.close();
        driver.quit();
        if (j == 1) {
            System.out.println("data inserted");
        } else {
            System.out.println("not inserted");
        }
    }
}

How can i get my output and remove this Exception?

Thanks in advance

4
  • you missing some jars on classpath, check you CP settings Commented May 5, 2014 at 11:33
  • i added Following jar Commented May 5, 2014 at 11:36
  • So, it fails only with Chrome Driver? Commented May 5, 2014 at 11:44
  • it fails with chrome and frifox Commented May 5, 2014 at 11:48

2 Answers 2

1

To check your code I tried the following:

@Test
public void test1() throws Exception {
    System.setProperty("webdriver.chrome.driver", "t:\\Others\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();

    String url = "http://www.upmandiparishad.in/commodityWiseAll.aspx";

    driver.get(url);
    Thread.sleep(5000);

    new Select(driver.findElement(By.id("ctl00_ContentPlaceHolder1_ddl_commodity"))).selectByVisibleText("Paddy");
    Thread.sleep(5000);

    driver.findElement(By.id("ctl00_ContentPlaceHolder1_txt_rate")).sendKeys("27/03/2014");
    Thread.sleep(5000);

    driver.findElement(By.id("ctl00_ContentPlaceHolder1_btn_show")).click();
    Thread.sleep(5000);

    driver.close();

}

It works fine with FireFox (29.0) and Chrome (34.0) drivers. I used the following jar-s:

  • selenium-server-standalone-2.41.0.jar
  • selenium-java-2.41.0.jar
  • junit-dep-4.11.jar
  • hamcrest-core-1.3.jar

You should check your project build path and add the appropriate jar-s. But you can find the missing class file in the selenium-server-standalone-2.XY.0.jar if you open it: selenium-server-standalone-2.41.0.jar\org\apache\http\entity\ContentType

Another way is to use httpcore.jar (httpcore-4.3.jar) that contains the missing class file.

Sign up to request clarification or add additional context in comments.

Comments

0

This is actually because you are using old version of the selenium java binding, i guess lower than 2.40.0 as this selenium version is missing org/apache/http/entity/ContentType (itself internally)

so there are two solutions to it.

Option 1- You wish to keep selenium 2.40.0 or lower

If you wish of keeping the old selenium 2.40.0 or lower then you have to add missing jar to your project yourself, if you are using maven below is the dependency.

<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3</version>
</dependency> 
                     or 

link to download the hhtpcore 4.3 jar

Option 2- You can update the old selenium 2.40.0 or lesser to 2.53.0 or latest 3.4.0

Please update to a new selenium version like selenium 2.53.0 or latest Selenium 3.4.0

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.53.0</version>
</dependency>

link for the selenium jar 2.53.0

                    **or** 

 <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.4.0</version>
    </dependency>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.