1

How to read data of each cell in Selenium WebDriver using Java?

I have tried with following code:

CSVReader reader = new CSVReader(new FileReader("D:/data1.csv"));
expectedLabels = reader.readNext();
FieldNames = reader.readNext();
FieldValues = reader.readNext();

4 Answers 4

3
File file = new File("D:/data.csv");
if(file.exists()){
 System.out.println("File Exists");
}
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;

while((line = bufRdr.readLine()) != null){
    StringTokenizer st = new StringTokenizer(line,",");
    col=0;
    while (st.hasMoreTokens()){
        //Following stroing the data of csv
    numbers[row][col] = st.nextToken();
    col++;
    }
    System.out.println();
    row++;
    }
    bufRdr.close();
Sign up to request clarification or add additional context in comments.

Comments

1

reading csv files become very easy with OPENCSV jar file. I have used this jar file couple of time to read file.

  1. We have predefined class called CSVReader, create an object and pass the csv file path

  2. call readAll() method which will return the csv content in List

  3. using Iterator, you can iterate all the values and use according to application

I have written article on this have a look it might help you.

http://learn-automation.com/how-to-read-csv-files-using-java/

1 Comment

Is it possible to paste the relevant code from the article here?
0

I am not able to provider string type variable in FileReader() function , it shows error while passing filereader() method with parameter in buffer reader fn

code shown below:

String f1= (System.getProperty("User.dir") + "\\Module9TestNG\\src\\TestLogin.xlsx");       
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String record; 
String url= null;

while ((record = bufRdr.readLine()) != null)
{
     String fields[] = record.split(",");
     url= fields[0].toString();
}

1 Comment

String f1= (System.getProperty("User.dir") + "\\Module9TestNG\\src\\TestLogin.xlsx"); BufferedReader bufRdr; bufRdr = new BufferedReader(new FileReader(file)); String record; String url= null; while ((record = bufRdr.readLine()) != null) { String fields[] = record.split(","); url= fields[0].toString(); }
0
   private String Fpath ="D:\\CkycApps.csv";   
    String line;
    File file = new File(Fpath);

    BufferedReader bufRdr;
    bufRdr = new BufferedReader(new FileReader(file));

    while((line = bufRdr.readLine()) != null){
        System.out.println(line);       
        String[] cell= line.split(",");
            String FirstName=cell[0];
            String MiddleName=cell[1];

}

1 Comment

Please add some explanation

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.