0

I am completely new to selenium as well as Java coding and I am trying to execute excel based login script -- Selenium webdriver java. I am having 2 problems :

  1. I am not able to iterate for loop to read all the records, only 1st row data is correctly processed and then gives nullpointerexception
  2. And my password for all the account is 123456 but selenium is passing it as 123456.0 and I am getting error due to this.

If any one has better scripts which actually run that is also acceptable since I have searched many code but I am not able to run then.

Below is my code :

public class Excel_Read_Write {


    public static void main(String[] args)throws Exception  
    {


        // TODO Auto-generated method stub
         System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
         WebDriver driver = new ChromeDriver();
    // To Maximize browser screen       
//       FileInputStream fs = new FileInputStream("C:\\LoginDetails.xls");
//          driver.manage().window().maximize();    
            String baseUrl = "http://openspace.website/vpms/public/";
            driver.get(baseUrl);
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);


//          String filePath = System.getProperty("user.dir")+"\\src\\excel_read_write";
            String filePath = System.getProperty("user.dir")+"\\src";
//          FileInputStream fs = new FileInputStream(filePath);

            String FilePath = "F:\\\\LoginDetails.xlsx";

            XSSFWorkbook wb = new XSSFWorkbook(FilePath);
            XSSFSheet s1 = wb.getSheetAt(0);
            int totalNoOfRows  = s1.getLastRowNum();
            int noOfColumns = s1.getRow(0).getPhysicalNumberOfCells();
            System.out.println(noOfColumns);
//          int totalNoOfCols = s1.getLastCellNum();
            System.out.println(noOfColumns);
            XSSFCell username,password;
             for(int i = 1; i < totalNoOfRows ; i++) {
                 for (int j=0; j < noOfColumns - 1; j++) {
                        username= s1.getRow(1).getCell(j);
                        password = s1.getRow(1).getCell(i);
                        System.out.println("Username"+username);
                        System.out.println("Password"+password);
                        driver.findElement(By.id("email")).clear();
                        driver.findElement(By.id("password")).clear();
                        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
                        driver.findElement(By.id("email")).sendKeys(username.toString());
                        driver.findElement(By.id("password")).sendKeys(password.toString());
                        driver.findElement(By.xpath("/html/body/div/div/div[3]/section/form/div[3]/button")).click();
                        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                         if ((driver.getPageSource().contains("Check In"))){
                                System.out.println("Successful Login verified for " +username.toString());
                                driver.findElement(By.xpath("//*[@id=\"bs-example-navbar-collapse-1\"]/ul/li[4]/a")).click();
                                driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                            } else {
                                System.out.println("Successful Login not verified for " +username.toString());
                            }
                 }
             }

Error message displayed :

Starting ChromeDriver 2.34.522940 (1a76f96f66e3ca7b8e57d503b4dd3bccfba87af1) on port 29074
Only local connections are allowed.
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Jan 09, 2018 12:28:43 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS
3
3
UsernameQA Master
Password1.0
Successful Login not verified for QA Master
Username1.0
Password1.0
Successful Login not verified for 1.0
UsernameQA Master
Passwordnull
Exception in thread "main" java.lang.NullPointerException
    at excel_read_write.Excel_Read_Write.main(Excel_Read_Write.java:69)

1 Answer 1

0

Solution:

  1. Change/replace the following lines,

    username= s1.getRow(1).getCell(j); password = s1.getRow(1).getCell(i);

    with

    username= s1.getRow(i).getCell(0); password = s1.getRow(i).getCell(1);

    also, remove/comment the inner for loop and it's closing brace. // for (int j=0; j < noOfColumns - 1; j++) { You gave the row as constant 1. that's why it is taking first row always.

  2. In excel column enter the password 123456 with quotes like '123456. It will format as string then you will not get the .0 at the end.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey Thanks a lot, my both problems got solved & I could run my script, Thank you very much..!!
Hey I am facing one more problem, script is skipping last row credentials
can you explain it
for(int i = 1; i <= totalNoOfRows ; i++) solved my problem
Hi Murthi, please check this my new question, based on addition to this script only, Please if see you can help me with this ad well. stackoverflow.com/questions/48181002/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.