0

I am trying to fetch data from a web site and I am able to do this but my problem is that when I fetch data of different commodity from site then I have to change every time in my code and I want to fetch data of following commodity at once how can I achieve my output

commodity

'Rice'
'Jwar'
'Corn'
'Matar'
'Chana'

Here is my code

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;


public class Getdata1 {

 public static void main(String args[]) throws {


 int j=0;


      WebDriver driver = new HtmlUnitDriver(BrowserVersion.getDefault());
        String sDate = "27/03/2014";
        String commodity="Jo";

        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(commodity);
         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"));
        String htmlTableText = findElement.getText();

    htmlTableText=htmlTableText.replace("S.No.DistrictMarketPrice","");
   htmlTableText= htmlTableText.replaceAll("\\s(\\d+\\s[A-Z])", "\n$1");
  htmlTableText = htmlTableText.replaceAll("(?=(.*?[ ]){4,}).*?[\n\r]", "");
   System.out.println(htmlTableText);


       driver.close();
        driver.quit();

    }
}
Daksh Shah
  • 2,997
  • 6
  • 37
  • 71
user3493831
  • 47
  • 1
  • 1
  • 8

1 Answers1

0

You perhaps need commodity sent as command line argument or as input from user.

Community
  • 1
  • 1
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
  • You have to replace `String commodity="Jo";` with `String commodity=args[0];` and pass "jo" or anyother desired commondity on command line. If you run application from command line, instead of Getdata1, run Getdata1 jo. If you are running it by double clicking the executable, you can either make multiple shortcuts and provide argument, or make empty files (without extension) with name jo, rice etc and drag it into exe file to launch it. Make sure to handle zero argument case. – Mohit Jain Apr 14 '14 at 07:23
  • is it possible using for loop with string array – user3493831 Apr 14 '14 at 07:33
  • Yes, you can do `for (String s: args) { System.out.println(s); }` – Mohit Jain Apr 14 '14 at 10:45
  • or `for(int i= 0;i < args.length;i++) { System.out.println(args[i]; }` – Mohit Jain Apr 14 '14 at 10:47