简体   繁体   English

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

[英]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. 我正在使用Selenium WebDriver,并且在以下给定的情况下无法使用。

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. 我有一个包含100个表行的页面,我需要获取每行的详细信息并将其写入Excel工作表。

So the number of columns in excel sheet will be 3 and number of rows will be 100. 因此,excel工作表中的列数将为3,行数将为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. 我需要将3列值存储在3个不同的变量中,并且需要将其传递给Excel Write操作代码。 Can some one provide a good logic for that .I am using poi framework for excel operation. 有人可以为此提供一个很好的逻辑吗?我正在使用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.");

This should work: 这应该工作:

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

Then create cells in the row: 然后在该行中创建单元格:

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

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

and so on... 等等...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Java for Selenium WebDriver在一个Excel工作表中编写2种不同的结果 - How to write the 2 different results in one excel sheet using java for selenium webdriver 数据驱动框架——如何使用Selenium WebDriver和java在excel表中读写 - Data Driven Framework — how to read and write in excel sheet using Selenium WebDriver with java 无法在 selenium webdriver 中写入 Excel 工作表 - Unable to write to Excel sheet in selenium webdriver 如何使用 apache poi 和 selenium webdriver 在 excel 文件中写入使用 List 和 Iterator 获得的值 - how to write values obtained using List and Iterator in a excel file using apache poi and selenium webdriver Selenium WebDriver-Java:如何使用Java将Webtable数据写入Selenium WebDriver中的excel文件? - Selenium WebDriver - Java : How to write webtable data into an excel file in Selenium WebDriver using Java? 在Selenium WebDriver中使用JAVA使用Excel工作表对不同网页上的内容进行数据驱动测试 - Data Driven testing using excel sheet for the content on different web pages using JAVA in Selenium WebDriver 无法使用Selenium Webdriver写入Excel - Unable to write to Excel using Selenium webdriver 如何使用Java在Selenium中已经打开的Excel工作表上书写 - How to write on already open excel sheet in Selenium using java 通过使用 java selenium 如何在 Excel 表中写入数据 - By using java selenium how to write data in Excel sheet 如何在XPath中通过引用传递值,在XPath中,值是从Selenium Webdriver中的外部Excel工作表导入的 - How to pass value by reference in xpath where the values are imported from external excel sheet in selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM