SeleniumWebDriver-Java:如何使用Java将webtable数据写入SeleniumWebDriver中的excel文件?
:)我正在尝试使用SeleniumWebDriver和Java将web表数据写入excel文件。通过下面的代码,我只能打印excel中的最后一列数据,而不能打印整个webtable数据。你能帮我一下吗SeleniumWebDriver-Java:如何使用Java将webtable数据写入SeleniumWebDriver中的excel文件?,java,excel,selenium,selenium-webdriver,Java,Excel,Selenium,Selenium Webdriver,:)我正在尝试使用SeleniumWebDriver和Java将web表数据写入excel文件。通过下面的代码,我只能打印excel中的最后一列数据,而不能打印整个webtable数据。你能帮我一下吗 import java.util.*; import java.lang.*; import java.io.*; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.X
import java.util.*;
import java.lang.*;
import java.io.*;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebTableTOSpreedsheet
{
public static void main(String[] args) throws IOException
{
System.out.println("Hello Dear.....");
System.out.println();
WebDriver wb = new FirefoxDriver();
wb.navigate().to("http://www.w3schools.com/html/html_tables.asp");
wb.manage().window().maximize();
System.out.println(wb.getTitle() +" - WebPage has been launched");
List<WebElement> irows = wb.findElements(By.xpath("//*[@id='main']/table[1]/tbody/tr"));
int iRowsCount = irows.size();
List<WebElement> icols = wb.findElements(By.xpath("//*[@id='main']/table[1]/tbody/tr[1]/th"));
int iColsCount = icols.size();
System.out.println("Selected web table has " +iRowsCount+ " Rows and " +iColsCount+ " Columns");
System.out.println();
FileOutputStream fos = new FileOutputStream("D://Software//AutomationPractise//WebTableTOSpreedsheet.xlsx");
XSSFWorkbook wkb = new XSSFWorkbook();
XSSFSheet sheet1 = wkb.createSheet("DataStorage");
for (int i=1;i<=iRowsCount;i++)
{
for (int j=1; j<=iColsCount;j++)
{
if (i==1)
{
WebElement val= wb.findElement(By.xpath("//*[@id='main']/table[1]/tbody/tr["+i+"]/th["+j+"]"));
String a = val.getText();
System.out.print(a);
XSSFRow excelRow = sheet1.createRow(i);
XSSFCell excelCell = excelRow.createCell(j);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a);
//wkb.write(fos);
}
else
{
WebElement val= wb.findElement(By.xpath("//*[@id='main']/table[1]/tbody/tr["+i+"]/td["+j+"]"));
String a = val.getText();
System.out.print(a);
XSSFRow excelRow = sheet1.createRow(i);
XSSFCell excelCell = excelRow.createCell(j);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a);
//wkb.write(fos);
}
}
System.out.println();
}
fos.flush();
wkb.write(fos);
fos.close();
}
}
import java.util.*;
导入java.lang.*;
导入java.io.*;
导入org.apache.poi.xssf.usermodel.XSSFCell;
导入org.apache.poi.xssf.usermodel.XSSFRow;
导入org.apache.poi.xssf.usermodel.xssfheet;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.openqa.selenium.By;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
公共类WebTableToPreedSheet
{
公共静态void main(字符串[]args)引发IOException
{
System.out.println(“您好,亲爱的…”);
System.out.println();
WebDriver wb=新的FirefoxDriver();
wb.navigate()到(“http://www.w3schools.com/html/html_tables.asp");
wb.manage().window().maximize();
System.out.println(wb.getTitle()+“-已启动网页”);
List irows=wb.findElements(By.xpath(“/*[@id='main']/table[1]/tbody/tr”);
int irowscont=irows.size();
List icols=wb.findelelements(By.xpath(“/*[@id='main']]/table[1]/tbody/tr[1]/th”);
int icolscont=icols.size();
System.out.println(“所选web表有“+iRowsCount+”行和“+iColsCount+”列”);
System.out.println();
FileOutputStream fos=新的FileOutputStream(“D://Software//automationpractice//WebTableTOSpreedsheet.xlsx”);
XSSFWorkbook wkb=新XSSFWorkbook();
XSSFSheet sheet1=wkb.createSheet(“数据存储”);
对于(int i=1;i您正在为(int j=1;j)的中的每个单元格创建新的空行
for (int i = 1; i <= iRowsCount; i++) {
XSSFRow excelRow = sheet1.createRow(i);
for (int j = 1; j <= iColsCount; j++) {
System.setProperty("webdriver.firefox.bin", "C:\\path\\to\\Mozilla Firefox\\firefox.exe");