簡體   English   中英

Selenium WebDriver-Java:如何使用Java將Webtable數據寫入Selenium WebDriver中的excel文件?

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

:)我正在嘗試使用帶有Java的Selenium-WebDriver將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();     
}
}

輸出: Excel中Webtable的數據寫入問題

您正在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");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM