如何使用poi和selenium(java)将Webtable数据写入xlsx
请任何人帮助我使用selenium和java将web表数据提取到XLSX文件中。我找不到解决方案…请告诉我thr是否是一种更好的方法 我的代码:如何使用poi和selenium(java)将Webtable数据写入xlsx,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,请任何人帮助我使用selenium和java将web表数据提取到XLSX文件中。我找不到解决方案…请告诉我thr是否是一种更好的方法 我的代码: List<WebElement> rows=sectiontable.findElements(By.tagName("tr")); Iterator<WebElement> i=rows.iterator(); int x=0; while(i.hasNext()) { WebElement row=i.
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
List rows=sectiontable.findElements(按.tagName(“tr”));
迭代器i=行。迭代器();
int x=0;
while(i.hasNext())
{
WebElement行=i.next();
列表列=行.findElements(按.tagName(“td”));
迭代器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);
系统输出打印项次;
//y=y+1;
如果(y==1){
this.c1=this.s;
系统输出打印LN(c1);
}
如果(y==2){
this.c2=this.s;
系统输出打印LN(c2);
}
如果(y==3){
this.c3=this.s;
系统输出打印项次(c3);
}
如果(y==4){
this.c4=this.s;
系统输出打印LN(c4);
}
如果(y==5){
this.c5=this.s;
系统输出打印项次(c5);
}int t=0;
对于(int u=1;u您到目前为止尝试了什么?我已经提取了web表并将其传递给hashMap。但是每次用正在提取的新数据替换该行时……请共享您编写的代码。List rows=sectiontable.findElements(By.tagName(“tr”);Iterator I=rows.Iterator();int x=0;while(I.hasNext()){WebElement row=i.next();List columns=row.findElements(By.tagName(“td”);迭代器j=columns.Iterator();int y=1;而(j.hasNext()){WebElement column=j.next();this.s=column.getText();