Search code examples
seleniumselenium-webdriverfile-ionullpointerexceptionfileinputstream

Selenium Webdriver | Exception while read username and password for gmail from excelsheet


Below is the exception I'm getting while trying to read username and password of gmail from the Excel file stored on the specific location, tried with adding excel in the project as well. Please help in resolving this issue.

Exception

  log4j:WARN No appenders could be found for logger (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager).
    log4j:WARN Please initialize the log4j system properly.
    log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
    Exception in thread "main" java.io.FileNotFoundException: D:\selenuim (Access is denied)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at SeleniumPackage.ExcelUtils.setExcelFile(ExcelUtils.java:45)
        at SeleniumPackage.ExcelUtils.main(ExcelUtils.java:36)

This is the code for reading username and password to login into gmail.

public class ExcelUtils {

    public static final String Path_TestData = "D:\\selenuim\\";
    public static final String File_TestData = "TestData.xls";
    public static HSSFSheet ExcelWSheet;
    public static HSSFWorkbook ExcelWBook;
    public static HSSFCell Cell;
    public static HSSFRow Row;  
    public static WebDriver  driver;

    public static void main(String[] args) throws Exception     {
                File pathToBinary = new File("C:\\Users\\sajjankumar.parjapat\\AppData\\Local\\Mozilla Firefox\\firefox.exe");      
        FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
        FirefoxProfile profile = new FirefoxProfile();
        WebDriver driver = new FirefoxDriver(ffBinary,profile);
        driver.manage().window().maximize();
        setExcelFile(Path_TestData,File_TestData);
        Login();
    }

    public static void setExcelFile(String path, String SheetName) throws Exception     {
        try         {
            FileInputStream ExcelFile = new FileInputStream(path);
            ExcelWBook = new HSSFWorkbook(ExcelFile);
            ExcelWSheet = ExcelWBook.getSheet(SheetName);
        }
        catch (Exception e) {
            throw (e);          }
    }

    public static String getCellData(int RowNum, int ColNum)        {
        try         {
            Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);
            String CellData = Cell.getStringCellValue();
            System.out.println(CellData);
            return CellData ; 
        }
        catch (Exception e) {
            throw (e);          }
    }

    public static void Login() throws InterruptedException      {
        String url = "https://www.google.com";
        driver.get(url);
        driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a")).click();
        Thread.sleep(2000);
        String Username = getCellData(1,1) ;
        String pwd = getCellData(1,2);
        driver.findElement(By.id("Email")).sendKeys(Username);
        Thread.sleep(2000);
        driver.findElement(By.id("next")).click();
        Thread.sleep(2000);
        driver.findElement(By.id("Passwd")).sendKeys(pwd);
        Thread.sleep(2000);
        driver.findElement(By.id("signIn")).click();
        Thread.sleep(2000);

        try         {
            Boolean oldVersion = driver.findElement(By.xpath(".//*[@id='browsers']/h2")).isDisplayed();
                    if(oldVersion)                      {
                        Thread.sleep(2000);
                        driver.findElement(By.xpath(".//*[@id='browsers']/p/b[2]/a")).click();
                    }
        }
        catch (Exception e) {
            throw e;            }
        Thread.sleep(5000);
        WebElement signOut = driver.findElement(By.xpath(".//*[@id='gb_71']"));
        signOut.click();
        Thread.sleep(2000);
    }
}

Solution

  • You need to give full path to the file as a parameter to the

    new FileInputStream(path)
    

    constructor, but you are giving only folder path (public static final String Path_TestData = "D:\selenuim\";)

    So change the line

    FileInputStream ExcelFile = new FileInputStream(path);
    

    to

    FileInputStream ExcelFile = new FileInputStream(path+SheetName);
    

    in the method setExcelFile