
Hello Welcome to Selenium Tutorial, in this post How to read CSV files using Java and how we can use into our Selenium script.
Selenium support only browser level automation and it does not have any API to read and write external data like Excel, Database so in previous post we have seen JExcel API and Apache POI.
What is CSV files.
CSV stands for comma separated values. Sometimes in your application you have to take data from existing csv files as well. Here is how csv files looks.

How to create CSV files
Open Notepad Enter Some values in format mention below and Save file as .csv format
“Automation”,”Selenium”,”Webdriver”
“Testing”,”Selenium”,”RC”
After saving files looks like

How to read CSV files
In this post we will use some third party API called opencsv, we can download this as jar file and can use existing methods to read file
download opencsv using below link
http://www.java2s.com/Code/Jar/o/Downloadopencsv23jar.htm

This will come as rar file extract this then you will find jar
Add that jar into project
How to add jar -Right click on project > Select Build path > Select configure build path> Add external jar> now Select the jar which we downloaded
Steps How to read-
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<String[]>
3-using Iterator, you can iterate all the values and use according to application
Program – How to read CSV files using Java
package blog; import java.io.FileReader; import java.util.Iterator; import java.util.List; import au.com.bytecode.opencsv.CSVReader; public class ReadCsvFiles { public static void main(String[] args) throws Exception { // This will load csv file CSVReader reader = new CSVReader(new FileReader("C:\\Users\\mukesh_otwani\\Desktop\\demo.csv")); // this will load content into list List<String[]> li=reader.readAll(); System.out.println("Total rows which we have is "+li.size()); // create Iterator reference Iterator<String[]>i1= li.iterator(); // Iterate all values while(i1.hasNext()){ String[] str=i1.next(); System.out.print(" Values are "); for(int i=0;i<str.length;i++) { System.out.print(" "+str[i]); } System.out.println(" "); } } }
Thanks for visiting my blog, Please comment below if you finding any issue while reading files.
Keep in touch. Have a nice day 🙂
Thanks it really helped me!!!
Thanks Devisha, I am glad it helped.
Does apache POI support CSV files??
Hi Hema,
You may need to used Apache Commons CSV library for same
Do you have any video for reading .csv file ??
Hi Hema,
Check this link. Hoppe this will help you.
Mukesh,
Hope you are doing well..
I have a quick question, I have designed page object factory framework. when we pass the argument in the main method for created method, do we have the possibility to pass the argument value using text file .The main idea is here not to pass the username ,password and other value in the in the main method.
Hi Manju,
I would recommend you to use properties/xml/json file instead of text file to read these kinds of data in your testing. You can have some file reader class to handle files to read data
It helped me.Thanks!
Hi Varun,
You’re most welcome…:)
How to search for a string and get the next element value in csv file
Hi Lakshmi,
As mentioned in blog post, iterate all List elements. While iteration, check for required string and whenever condition gets satisfied, iterate once more and break loop.
HI makes i had an issue with mac os using visual studio can u pls how to work on this using C# rather than java
thanks in advance
Hi Reddi,
Extremely sorry, I never worked with C# for selenium. If it is Java then I can surely help you. You are always welcome to learn-automation.com
hi mukesh can u show how the data will call from csv file to fill the registration forms(like registring a user in to any application gmail,facebook)
HI Prasad,
Below link will help http://www.software-testing-tutorials-automation.com/2015/04/data-driven-test-using-csv-file-in.html
hi Mukesh,
is there any way to read data from csv file like excel file, based on column and row index
?? plz let me know if there is any way
Hi Harvir,
Yes you can do that but for that you have to write your own code.
HI Mukesh,
Gud evng,
What is use of csv file, In real time project where we can use, it is rerally important to learn.can u expn clearly.
Hey Ranjith,
It depends on org some companies use excel to read data and some use csv file 🙂
Hi,
Why we are introducing complexity of external jar here? We can directly read this file line by line as CSV is normal text file and then use StringTokenizer class (inbuilt in Java) to split every line with comma(,) as delimiter and use individual elements as cells of excel.
Hi Akshay,
You can do using pure java code as well. I showed using OpenCSV jars.
Can you show a simple program for us to move the csv file parameters to constructor..!!
Hey Vamshi,
I applied the same concept in reading excel file post. Kindly check and try to apply the same logic.
Thumbs up for ur presentation : )
” List li=reader.readAll();”
Why we are using list here since we can directly store in a string array?
Hi Amit,
It all depends on return type of method. List is part of collection interface and it allow dynamic element to be stored.
Where is the au.com.bytecode.opencsv.CSVReader??
Hi Naga,
It is coming from the jar which we have to download..