How to read CSV file in Selenium JAVA WebDriver?

How to read CSV file in Selenium JAVA WebDriver?

1. Go to http://sourceforge.net/projects/opencsv/ and download the opencsv jar file. Add the Jar file to your JAVA project.

2. Use the following Libraries.

import com.opencsv.CSVReader;
import java.io.FileReader;

Sample Code:

String CSV_PATH = "D:\\demo.csv";
CSVReader reader = new CSVReader(new FileReader(CSV_PATH));
String [] csvCell;
while ((csvCell = reader.readNext()) != null) {   
    String acctName = csvCell[0];
    String acctType = csvCell[1];
    String acctIndustry = csvCell[2];
    String acctSource = csvCell[3];
    String acctRating = csvCell[4];
    String acctOwnership = csvCell[5];
    String acctActive = csvCell[6];
    String acctPriority = csvCell[7];
    String billingStreet = csvCell[8];
    String billingCity = csvCell[9];
    String billingState = csvCell[10];
    String billingPostalCode = csvCell[11];
    String billingCountry = csvCell[12];
    String mailingStreet = csvCell[13];
    String mailingCity = csvCell[14];
    String mailingState = csvCell[15];
    String mailingPostalCode = csvCell[16];
    String mailingCountry = csvCell[17];
}

Leave a Reply