• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Selenium Framework / Read excel file in selenium using JExcel

Read excel file in selenium using JExcel

March 15, 2015 by Mukesh Otwani 43 Comments

Read excel file in selenium

Hello Welcome in this post we will see how to Read excel file in selenium using Jexcel.

As we, all know Selenium support only browser automation, so for reading and writing with Excel files we have to user third party API like JExcel and Apache POI.

JExcel- JExcel is free API to work with Excel files; it comes with predefined method, classes and interface, which we can directly use.

Limitation of JExcel- It only support .xls files it means files whose extension is .xls files 

 

some useful classes and interface, which we will be using
Workbook (Class) – Will handle workbook.

Sheet (Interface) – Which will handle sheets

Cell (Interface) – Which will handle cell

Download JExcel API

Open any browser and navigate to below URL

http://mirrors.ibiblio.org/pub/mirrors/maven/net.sourceforge.jexcelapi/jars/jxl-2.6.jar

 

Download zip file and extract the same. After extraction, you will get jexcel.jar file.

 

Read excel file in selenium
Read excel file in selenium

 

 Add JExcel jar to project

Right Click on project then Click on Build path> then Click on configure build path then Go to Library Section then
Click on add external jars and now attach jar file click on Apply finally click on  Save button

 

Program- Read excel file in selenium-  Youtube

Create a separate package  into src package

ExcelRead> create a new Class TestRead

package ExcelRead;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.testng.annotations.Test;

public class TestRead {

@Test
public void TestReadData(){

// You need to use File class which will ask for file location.I specified  base// directory //using dot(.) operator then inside data folder I have testdata.xls// stored

File src=new File("./data/testdata.xls");

try {
// Workbook is a class in Jexcel which will take file as an argument and getWork//book is a predefined method which will read the workbook and will return the w//Workbook object

Workbook wb=Workbook.getWorkbook(src);

// Workbook is loaded now we have to load sheet so using workbook object (wb) we// can call getSheet method which will take index as an argument and will load t//he sheet, we can also specify the sheetname also

Sheet sh1=    wb.getSheet(0);

// Sheet is loaded then we have to read cell so using sh1 object call getCell me//thod which we take two arguments getCell(column,row)

Cell  c1=sh1.getCell(0,0);

//Cell is loaded then using getContents method we have  to extract the data usin//g getContents() methods
// this method will always return you String.
// now you are done

String data1=c1.getContents();

/ /Print the data
System.out.println(data1);

System.out.println("Sheet data is "+data1);

} catch (BiffException e) {

e.printStackTrace();

} 
catch (IOException e)
{            
e.printStackTrace();
}

}

Thanks for visiting my blog. Keep in touch.

Filed Under: Selenium Framework Tagged With: Read External files

Reader Interactions

Comments

  1. Arhun says

    September 26, 2019 at 3:17 PM

    How to integrate bamboo with eclipse

    Reply
    • Mukesh Otwani says

      September 26, 2019 at 5:19 PM

      Hi Arhun,

      Kindly check with Bamboo APIs https://docs.atlassian.com/atlassian-bamboo/REST/5.5.0/

      Reply
  2. sumana s says

    September 16, 2019 at 11:49 AM

    How can i select drop down values from excel sheet? I have to select gender and rest of the values are being passed from excel sheet. kindly suggest

    Reply
    • Mukesh Otwani says

      September 16, 2019 at 3:32 PM

      Hi Sumana,

      Using Apache POI or JExcel, read data from excel and save it to some variable. Then pass this variable as argument to Select class defined method where you can call selectClassObj.selectByVisibleText(variable_containing_value_from_excel_cell);

      Reply
      • Sumana S says

        September 16, 2019 at 3:57 PM

        Thanks mukesh. Got it. i have one more problem. how can i read all the rows and columns from excel? Actually i need to create 100 users to check for load testing. i have excel file with 100 users with name,email id, user role and gender. how can i create user by reading those files at a time? kindly help with a sample code to understand

        Reply
        • Mukesh Otwani says

          September 16, 2019 at 4:23 PM

          Hi Sumana,

          If you are using TestNG framework then you can use @dataProvider annotation. I have posted a scenario in which I am using two different fb credentials. You can use this mechanism. Please check this link http://learn-automation.com/data-driven-framework-in-selenium-webdriver/

          Reply
          • Sumana S says

            September 20, 2019 at 1:13 PM

            Hi Mukesh, how can we come across when we have same xpaths for different p tags. i create a nugget in which i need to add object. it is working fine. when i add next nugget and try to click on add object it is not discovering the element as add object of 1st nugget is also having same path as of second. kindly help

          • Mukesh Otwani says

            September 20, 2019 at 3:45 PM

            Hi Sumana,

            p tags stands for paragraph in html language. On canvas, if you keep on adding new line, p tags also keep on incrementing. So there ultimately you won’t get any concrete locator in this case. I recommend you to not use p tags while xpath creation

  3. saurav kumar says

    February 11, 2017 at 11:43 AM

    Hello Mukesh,
    I went for adp interview there they asked me how to read data from excel but the condition is that even if position of column is change then also it will read the correct data from excel .Let say the position of 2nd column move to 3rd column . so they told me to write the read excel in dynamic way

    Reply
    • Mukesh Otwani says

      February 16, 2017 at 11:43 AM

      Hi Saurav,

      You can set a loop for column increment in order to read data.

      Reply
  4. saikrishna says

    February 7, 2017 at 6:43 PM

    HI Mukesh,

    Can we write data to excel using JExcel API, like we do it using Apache POI.

    Reply
    • Mukesh Otwani says

      February 7, 2017 at 7:39 PM

      Hi Saikrishna,

      Yes, JExcel provides support for both read & write.

      Reply
  5. Sudha says

    February 2, 2017 at 7:34 PM

    how to pass xpath value read from excel file to the driver.findElement()?

    Reply
    • Mukesh Otwani says

      February 2, 2017 at 10:18 PM

      Hi,

      Whatever cell content you are reading from excel pass it as argument to driver.findElement(By.xpath()). For this you need to set some loop or define row, column combination in order to read data from excel.

      Reply
      • Sudha says

        February 6, 2017 at 3:26 PM

        Hi Mukesh,
        If i have By.name(“username”) in excel file and i read it as string value but i want to pass it as parameter to driver.findElement(—-) .. String can’t be typecasted to By. can you help me please?

        Reply
        • Mukesh Otwani says

          February 8, 2017 at 2:36 PM

          Hi Sudha,

          As of now it is not possible to typecast String value to By. You need to create a method in your framework which can handle values from excel and pass required locator value corresponding to its type such xpath, id, linktext etc.

          Reply
  6. Vaishnavi says

    January 28, 2017 at 12:26 PM

    Hi I want to take inputs from excel sheet and feed it to username or passwords field in UI. Can you please help me with that

    Reply
    • Mukesh Otwani says

      January 28, 2017 at 5:53 PM

      Hi Vaishnavi,

      This is what I mentioned in my post. Whatever data you read from excel pass it as it is to sendKeys parameter against webelement for username and password fields respectively.

      Reply
  7. Akansha Pasricha says

    November 29, 2016 at 5:33 PM

    Thanks alot very helpful post.

    Reply
    • Mukesh Otwani says

      November 30, 2016 at 8:06 PM

      Welcome dear. keep visiting and let me know for any help.

      Reply
  8. nidhi says

    October 27, 2016 at 2:51 AM

    Sir, thanks a tonn for these useful tutorials. very well explained. I am beginner for Selenium.
    Can we write excel files using JExcel API?

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 10:57 PM

      Hi Nidhi,

      Thank you. I would suggest you to go throw Apache POI which is used in market.

      Kindly go through below post and let me know if any help required.

      http://learn-automation.com/readwrite-excel-files-in-selenium/

      Reply
  9. kanchana says

    October 13, 2016 at 12:46 AM

    hi mekesh sir, In data driven testing you hard coded the columns, could you please give me the code for DDT ratherthan hardcoding for column?

    Reply
    • Mukesh Otwani says

      October 13, 2016 at 8:46 AM

      HI,

      You can use getPhysicalCellNumber to get Column count dynamically.

      Reply
  10. Ram Narayan says

    October 3, 2016 at 3:13 PM

    Sir i am getting the “jxl.read.biff.BiffException” exception. I think the file is incompatible as i’ve changed the extension manually from xlsx to xls. Am i correct ? how can i test my piece of code.

    Reply
    • Mukesh Otwani says

      October 3, 2016 at 4:11 PM

      Hi Ram,

      kindly go through below post as well http://learn-automation.com/readwrite-excel-files-in-selenium/

      Reply
  11. Heena Kothari says

    September 21, 2016 at 11:45 AM

    Thanks a ton dear for sharing your knowledge.

    Reply
    • Mukesh Otwani says

      September 23, 2016 at 11:29 PM

      Your most welcome Heena

      Reply
  12. Brahma says

    September 20, 2016 at 3:37 PM

    HI Mukesh,

    After a lot of searching in the internet, I found a very good website to learn selenium and Iam following your youtube videos regularly. Thanks for your information.
    I have one doubt, I want to read a data from Excel(.xls file) using Apache poi. I dont want to use Jxcel jar.
    please post a code for that.

    Reply
    • Mukesh Otwani says

      September 29, 2016 at 2:28 PM

      Yes Brahma, Here is the link http://learn-automation.com/readwrite-excel-files-in-selenium/

      Reply
  13. Arcillas Jether says

    September 12, 2016 at 9:19 PM

    Excellent tutorial .. kudos bro 🙂

    Reply
    • Mukesh Otwani says

      September 12, 2016 at 10:56 PM

      Thanks Arcillas

      Reply
  14. Kiran Bagisetty says

    September 12, 2016 at 6:56 AM

    Hello Mukesh,

    After a lot of search, I think landed at the right place. Thanks a ton for your info and training. I would be a frequent visitor to your site.

    Reply
    • Mukesh Otwani says

      September 12, 2016 at 2:51 PM

      Thanks Kiran 🙂 keep learning and let me know if any doubt.

      Reply
  15. Ravi says

    July 14, 2016 at 11:12 AM

    Thanks for the information. It is very useful……………..

    Reply
    • Mukesh Otwani says

      July 14, 2016 at 12:40 PM

      Your most welcome Ravi and let me know if any help from my side.

      Reply
  16. Fan says

    July 2, 2016 at 3:22 PM

    Thanks Mukesh

    Reply
  17. Priya says

    April 2, 2016 at 4:59 AM

    Its very clear and informative…Thanks for posting this video

    Reply
    • Mukesh Otwani says

      April 4, 2016 at 11:52 PM

      Thanks Priya 🙂 Let me know if any help required from my end.

      Reply
  18. Maheeh says

    January 1, 2016 at 1:03 PM

    Hi sir
    how to a program using excel sheet but that program should be in one method only please guide me sir

    Reply
    • Mukesh Otwani says

      January 2, 2016 at 10:34 PM

      Hi Maheeh,

      Please follow below video https://www.youtube.com/watch?v=sbBdj4zIMqY

      Reply
  19. paramesh says

    October 1, 2015 at 6:56 AM

    Excellent work…Great Explanation…THANKS A LOTTT

    Reply
    • Mukesh Otwani says

      October 1, 2015 at 9:34 AM

      Thanks Paramesh 🙂 Keep visiting

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

Video Player
https://www.youtube.com/watch?v=w_iPCT1ETO4
00:00
00:00
31:21
Use Up/Down Arrow keys to increase or decrease volume.

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?