如何使用selenium webdriver将3个不同变量中的3列值写入Excel工作表

时间:2015-08-17 08:43:51

标签: java excel selenium

我正在使用Selenium WebDriver,我被下面给出的方案阻止了。

测试场景

我有一个包含100个表行的页面,我需要获取每行的详细信息并将其写入excel表。

因此,Excel工作表中的列数将为3,行数将为100。

我需要将3个列值存储在3个不同的变量中,我需要将它传递给Excel Write操作代码。 有人可以提供一个很好的逻辑。我正在使用poi框架进行excel操作。

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.");

1 个答案:

答案 0 :(得分:1)

这应该有效:

row = sheet.createRow(rowNum) //在此处指定行号

然后在行中创建单元格:

row.createCell(1)// cell 1

row.createCell(2)// cell 2

依旧......