:) Java와 함께 Selenium-WebDriver를 사용하여 웹 테이블 데이터를 Excel 파일에 쓰려고합니다. 아래 코드로 Excel의 마지막 열 데이터 만 인쇄 할 수 있지만 전체 웹 테이블 데이터는 인쇄 할 수 없습니다. 여기 좀 도와 주 시겠어요 .....
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();
}
}
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");
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다