如何使用poi和selenium将Webtable数据写入xlsx(java)

时间:2015-11-26 06:05:35

标签: java selenium selenium-webdriver

任何人都可以帮助我使用selenium和java将网页数据提取到 XLSX 文件中。我无法找到解决方案...让我知道如果thr是更好的方法这样做

我的代码:

List<WebElement> rows=sectiontable.findElements(By.tagName("tr")); 
Iterator<WebElement> i=rows.iterator(); 

int x=0; 
while(i.hasNext())
{   
    WebElement row=i.next(); 
    List<WebElement> columns=row.findElements(By.tagName("td")); 
     Iterator<WebElement> j=columns.iterator(); int y=1; while(j.hasNext())        
       {         
     WebElement column=j.next(); this. s=column.getText();
  } System.out.println("x=" +"" +x);
            System.out.println("y=" +""+y);
            System.out.println(s);


            //y=y+1;
            if(y==1){
                this.c1= this. s;
                System.out.println(c1);
            }
            if(y==2){
                this.c2= this. s;
                System.out.println(c2);
            }
            if(y==3){
                this.c3= this. s;
                System.out.println(c3);
            }

        if(y==4){
            this.c4= this. s;
            System.out.println(c4);
        }
        if(y==5){
            this.c5= this. s;
            System.out.println(c5);
        }   int t=0;
        for(int u=1;u<this.tablerows;u++){
        HashMap<Integer, Object[]> data = new HashMap<Integer, Object[]>();
        //String str=Integer.toString(u);
        data.put(t++, new Object[] {t,this.c1, this.c2, this.c3,this.c4,this.c5});

        //data.put("1", new Object[] {"s1","s2"});
        //data.put("2", new Object[] {"s3","s4"});
         Set<Integer> keyset = data.keySet();
            int rownum = 0;
            for (Integer key : keyset)
            {
                Row row1 = sheet.createRow(rownum++);
                Object [] objArr = data.get(key);
                int cellnum = 0;
                for (Object obj : objArr)
                {
                    Cell cell=row1.createCell(cellnum++);
                   if(obj instanceof String)
                        cell.setCellValue((String)obj);
                    else if(obj instanceof Integer)
                        cell.setCellValue((Integer)obj);
                   //Write the workbook in file system
                    FileOutputStream out = new FileOutputStream(new File("D://demo1.xlsx"));
                    workbook.write(out);
                }
            }
        }
          y=y+1;
        }
    x=x+1;
}
        try
        {
            //Write the workbook in file system
          //  FileOutputStream out = new FileOutputStream(new File("D://demo1.xlsx"));
            //workbook.write(out);


            //out.close();
            System.out.println("demo.xlsx written successfully on disk.");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
This is the method for featching data from table that is being identified

0 个答案:

没有答案
相关问题