Reading data from Excel in my Selenium test

0 votes

I am trying to read the username and password credentials from an excel file. The error i get is:

log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager). log4j:WARN Please initialize the log4j system properly.

And my code is: 

public static void main(String[] args) throws BiffException, IOException {      
    Sheet s;
    WebDriver driver = new FirefoxDriver();
    FileInputStream fi = new FileInputStream("E:\\myExcelWorkBook.xls");
    Workbook W = Workbook.getWorkbook(fi);

    s = W.getSheet(0);

    for(int row = 0;row <= s.getRows();row++){

    String Username = s.getCell(0,row).getContents();
    System.out.println("Username" +Username);
    driver.get("AppURL");
    driver.findElement(By.id("txtUserName")).sendKeys(Username);

    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);

    driver.findElement(By.id("txtPassword")).sendKeys(password);

    driver.findElement(By.id("btnLogin")).click();
   }
 }

What could possibly be the problem?

Apr 6, 2018 in Selenium by Martin
• 4,220 points
3,715 views

1 answer to this question.

0 votes

Log4j problems are usually thrown because of missing dependencies. Can you please re-download all Apache-POI jars, place them along with Selenium dependencies and try again?

Also, I tweaked the code a bit. Check of this works for you. It did for me.

package com.test.utitlity;
import java.io.IOException;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class readExcel extends globalVariables {

    /**
     * @param args
     * @throws IOException 
     */
    public static void readExcel(int rowcounter) throws IOException{

        XSSFWorkbook srcBook = new XSSFWorkbook("./prop.xlsx");     
        XSSFSheet sourceSheet = srcBook.getSheetAt(0);
        int rownum=rowcounter;
        XSSFRow sourceRow = sourceSheet.getRow(rownum);
        XSSFCell cell1=sourceRow.getCell(0);
        XSSFCell cell2=sourceRow.getCell(1);
        XSSFCell cell3=sourceRow.getCell(2);
        System.out.println(cell1);
        System.out.println(cell2);
        System.out.println(cell3);

}
}
answered Apr 6, 2018 by nsv999
• 5,070 points

Related Questions In Selenium

0 votes
1 answer

Error in for loop to fetch data from excel in selenium

You are closing theFileInputStream inside the for-loop. ...READ MORE

answered Jun 20, 2018 in Selenium by Samarpit
• 5,130 points
336 views
0 votes
1 answer
0 votes
1 answer

Read data from excel sheet in python

Try to use xrld. A python module used ...READ MORE

answered Jun 28, 2018 in Selenium by Samarpit
• 5,130 points
630 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,070 points
437 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
787 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
431 views
0 votes
1 answer

Can anyone help me that how to run Selenium WebDriver test cases in Chrome?

Download the executable driver from: ChromeDriver Download Before ...READ MORE

answered Mar 29, 2018 in Selenium by nsv999
• 5,070 points
208 views
0 votes
1 answer

How do i change the location where my file gets downloaded in Selenium Webdriver and firefox driver?

There are a couple of errors there. ...READ MORE

answered Apr 12, 2018 in Selenium by nsv999
• 5,070 points
1,424 views