我正在使用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.");

最佳答案

这应该工作:

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

然后在该行中创建单元格:

row.createCell(1)//单元格1

row.createCell(2)//单元格2

等等...

10-07 19:30
查看更多