BABURAJ VD
BABURAJ VD

Reputation: 69

How to write the 3 column values in 3 different variables into an Excel sheet using selenium webdriver

I am working with Selenium WebDriver and I am blocked with the below given scenario.

Test Scenario

I have a page containing 100 table rows and I need to fetch details of each rows and write it to an excel sheet.

So the number of columns in excel sheet will be 3 and number of rows will be 100.

I need to store the 3 column values in 3 different variables and I need to pass it to the Excel Write operation code. Can some one provide a good logic for that .I am using poi framework for excel operation.

Here are the steps
------------------

 - Step 1: Navigate to the page containing 100 rows (Say Page 1)
 - Step 2: Click on the first rows 
 - Step 3: Navigate to the details page of that rows 
 - Step 4: Fetch the details from page such as 'Name','Email', 'Address'          
 - Step 5: Write the 'Name', 'Email', 'Address' date to
   an excel sheet in 3 different columns 
 - Step 6: Navigate back to the Page 1. 
 - Step 7:Click on the 2ND Row and Continue the step 3 to 6
 - Step 8:Exit the loop after getting the 100 rows and writing to excel
       sheet.

    Code I have Tried 
    -----------------
    //To find the Parent Table

     Element =driver.findElement(By.className("LevelExtreme"));
            System.out.print("True 2");

    //To find the Table Row

            List<WebElement> list= Element.findElements(By.xpath(".//tr[@class='MouseOverOut']"));
            System.out.println("countRows="+list.size());
            countRows=countRows+1;
            //driver.switchTo().defaultContent();
            Thread.sleep(4000);

    //For Loop for performing Number of Rows in the table and Iterating to each element

            for(int ExcelRow=0;ExcelRow<=countRows;ExcelRow++)
            {

                 Thread.sleep(5000);    
                 System.out.println("Row=" +ExcelRow);
                 Element =driver.findElement(By.className("LevelExtreme"));
                 System.out.println("i Value="+ExcelRow);
                 int j=ExcelRow+3;
                 Element.findElement(By.xpath("//table[@id='LevelExtreme']/tbody/tr["+j+"]/td[3]/a")).click();


//To get the details of each member 
                 getDetailsOfMembers();

                 System.out.println("J Value="+j);
                 Thread.sleep(5000);

                 driver.navigate().back();
                 Thread.sleep(15000);
                 System.out.println("Back Button Pressed("+ExcelRow+")");
            }

        }


        public void getDetailsOfMembers() throws EncryptedDocumentException, InvalidFormatException, IOException
        {


          //Fetching the details of each member
            Element= driver.findElement(By.xpath(".//*[@id='Form1']/table[2]/tbody/tr/td[2]"));

            String Name=    Element.getText();    

            Element= driver.findElement(By.xpath(".//*[@id='Form1']/table[3]/tbody/tr[1]/td[3]"));

            String Member_ID=   Element.getText();

            Element= driver.findElement(By.xpath(".//*[@id='Form1']/table[3]/tbody/tr[3]/td[3]"));

            String Email_ID=    Element.getText();

Datas[ExcelRow][0]=Name;
        Datas[ExcelRow][1]=Member_ID;
        Datas[ExcelRow][2]=Email_ID;

    //Excel Write operation

            FileInputStream fis=new FileInputStream("E:\\ExcelRead.xls");

            Workbook wb=WorkbookFactory.create(fis);

            Sheet sh=wb.getSheet("Input");


                 //writing the content to 3 column(Here is where I am stuck)

    Row row=sh.createRow(ExcelRow);

                Cell cell_1=row.createCell(1);

                cell_1.setCellValue(Name);


                Cell cell_2=row.createCell(2);



                cell_2.setCellValue(Member_ID);


                Cell cell_3=row.createCell(3);  

                cell_3.setCellValue(Email_ID);

    //Excel write operation code 

         FileOutputStream fos=new FileOutputStream("E:\\ExcelRead.xls");

    // write operation code        



                   wb.write(fos);
                    fos.close();
                    System.out.println("Excel File Written.");

Upvotes: 0

Views: 2237

Answers (1)

rabia
rabia

Reputation: 106

This should work:

row=sheet.createRow(rowNum) // specify row number here

Then create cells in the row:

row.createCell(1) // cell 1

row.createCell(2) // cell 2

and so on...

Upvotes: 1

Related Questions