我在网络表中有近1500 rows
,我想将所有行数据写入excel。
我正在使用Selenium-Webdriver,Java
。问题是程序在写入近300行后没有任何错误就停止了。
请帮助我,如何将所有数据写入excel。如果我给thraed.sleep(5000);
花费更多时间。
我使用了下面提到的代码:
List<WebElement> irows = a2.findElements(By.xpath("//*[@id='filter_result']/table/tbody/tr/td[2]/a[1]"));
int iRowsCount = irows.size();
System.out.println(iRowsCount);
FileOutputStream fos = new FileOutputStream("E:\\jega\\testw3.xlsx");
XSSFWorkbook wkb = new XSSFWorkbook();
XSSFSheet sheet1 = wkb.createSheet("DataStorage");
String a3=null;
for (int i=500,Row=0;i<=iRowsCount;i++) {
try
{
WebElement val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[2]"));
// WebElement val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td["+j+"]"));
String a = val.getText();
if( a.length() != 0 )
{
a3=a;
//int length = val.length();
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
else
{
System.out.print(a3 + '\n');
XSSFRow excelRow = sheet1.createRow(Row++);
XSSFCell excelCell = excelRow.createCell(0);
excelCell.setCellType(XSSFCell.CELL_TYPE_STRING);
excelCell.setCellValue(a3);
for ( int j=5;j<12;j++)
{ switch (j)
{
case 5:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[5]"));excelCell = excelRow.createCell(1);
excelCell.setCellValue(val.getText()); break;
case 8:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[8]"));excelCell = excelRow.createCell(2);
excelCell.setCellValue(val.getText());break;
case 9:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[9]"));excelCell = excelRow.createCell(3);
excelCell.setCellValue(val.getText());break;
case 11:val= a2.findElement(By.xpath("//*[@id='filter_result']/table/tbody/tr["+i+"]/td[11]"));excelCell = excelRow.createCell(4);
excelCell.setCellValue(val.getText());break;
}
}
Thread.sleep(5000);
}
}//tryc
catch (NoSuchElementException e) {
}//cath
}//for
// System.out.println();
// Thread.sleep(6000);
fos.flush();
wkb.write(fos);
fos.close();
有人可以帮我解决这个问题吗? 非常感谢您的帮助 最诚挚的问候。
答案 0 :(得分:0)
我还将网表数据导出到这样的excel表, 这对我来说很好。希望这会对你有所帮助。
WebElement element = driver.findElement(By.xpath("//*[@id='entireBody']/div[3]/div/table"));
List<WebElement> tbdy = element.findElements(By.tagName("tbody"));
List<WebElement> thed = element.findElements(By.tagName("thead"));
Thread.sleep(1000);
List<WebElement> tr = tbdy.get(0).findElements(By.tagName("tr"));
List<WebElement> trh = thed.get(0).findElements(By.tagName("tr"));
Thread.sleep(1000);
try {
String filename = Path;
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("FirstSheet");
CellStyle style = workbook.createCellStyle();//Create style
Font font = workbook.createFont();//Create font
font.setBoldweight(Font.BOLDWEIGHT_BOLD);//Make font bold
style.setFont(font);//set it to bold
HSSFRow row = sheet.createRow(0);
for(int i = 0; i < trh.size(); i++){
List<WebElement> tdh =trh.get(i).findElements(By.tagName("td"));
System.out.println("loop1");
for (int j = 0; j < tdh.size(); j++) {
row.createCell(j).setCellValue(tdh.get(j).getText());
row.getCell(j).setCellStyle(style);
}
}
for (int i = 0; i < tr.size(); i++) {
HSSFRow row2 = sheet.createRow(i+1);
List<WebElement> td = tr.get(i).findElements(By.tagName("td"));
for (int j = 0; j < td.size(); j++) {
td.get(j).getText();
row2.createCell(j).setCellValue(td.get(j).getText());
}
}
FileOutputStream fileOut = new FileOutputStream(filename);
workbook.write(fileOut);
fileOut.close();
System.out.println("Your excel file has been generated!");
&#13;