gpt4 book ai didi

java - Selenium WebDriver - Java : How to write webtable data into an excel file in Selenium WebDriver using Java?

转载 作者:行者123 更新时间:2023-12-04 21:02:13 26 4
gpt4 key购买 nike

:) 我正在尝试使用 Selenium-WebDriver 和 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.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();
}
}

输出: Webtable's data write issue in excel

最佳答案

您正在为 for (int j=1; j<=iColsCount;j++) 中的每个单元格创建新的空行环形。移动XSSFRow excelRow = sheet1.createRow(i);循环迭代 i .

    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");

关于java - Selenium WebDriver - Java : How to write webtable data into an excel file in Selenium WebDriver using Java?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34150116/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com