2016-03-04 49 views
0

問題 下面的代碼可以讓我要麼依靠,如果我選擇了「日」打印列標題或打印網頁表格數據到一個CSV文件或'td'標籤,但不能同時使用。如何打印沿側表數據的列標題由網絡與webdriver的出類拔萃

我的問題 如何在我的CSV輸出中同時打印到csv'th'和'td'文本?

代碼 我已經試過2個版本的我的代碼,但結果是連接same.Both版本。

代碼版本1

public class WebToCSV { 

static WebDriver driver = new FirefoxDriver(); 

    public static void main(String[] args) throws Throwable { 

     //driver.navigate().to("http://goo.gl/El1PIV"); 
     driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures"); 
     driver.manage().window().maximize(); 
     driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

     WebElement table = driver.findElement(By.cssSelector 
       ("div[class='data-tables first']")); 

     List<WebElement> irow = table.findElements 
       (By.cssSelector("div[class='data-tables first'] tr")); 
     System.out.println("No. of rows in the table are: " + irow.size()); 

     // Create excel workbook and sheet. 
     FileOutputStream fos = new FileOutputStream 
       ("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv"); 

     XSSFWorkbook wb = new XSSFWorkbook(); 
     XSSFSheet ws = wb.createSheet("WriteToXL"); 

     for(int r=0; r<irow.size(); r++) { 
      WebElement webRow = irow.get(r); 
      System.out.println(webRow.getText()); 
      XSSFRow row = ws.createRow(r); 
      List<WebElement> allCells = webRow.findElements(By.tagName("td")); 

      for(int c=0; c<allCells.size(); c++) { 
       WebElement webCell = allCells.get(c); 
       String text = webCell.getText(); 

       XSSFCell excelCell = row.createCell(c); 
       excelCell.setCellValue(text); 
      } 

      System.out.println(""); 
     } 

     fos.flush(); 
     wb.write(fos); 
     fos.close(); 

     end(); 

} 
    public static void end() { 
     driver.close(); 
     driver.quit(); 
    } 

}

代碼版本2 此版本具有2套for循環,第一個打印列標題中CSV和第二個到打印來自webtable的所有數據。

public class StatsToxL { 

static WebDriver driver = new FirefoxDriver(); 

public static void main(String[] args) throws Throwable { 

    //driver.navigate().to("http://goo.gl/El1PIV"); 
    driver.navigate().to("http://www.bloomberg.com/markets/stocks/futures"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 

    WebElement table = driver.findElement(By.cssSelector 
      ("div[class='data-tables first']")); 

    // Get webtable header column row. 
    List<WebElement> irow2 = table.findElements 
      (By.cssSelector("div[class='data-tables first'] thead tr ")); 
    System.out.println("No. of rows in the header are: " + irow2.size()); 

    // Get all webtable rows 
    List<WebElement> irow = table.findElements 
      (By.cssSelector("div[class='data-tables first'] tbody tr")); 
    int iRowCount = irow.size(); 
    System.out.println("No. of rows in the table are: " + iRowCount); 

    // Create excel workbook and sheet. 
    FileOutputStream fos = new FileOutputStream 
      ("/Users/HARSHENDU/Desktop/Selenium_Practice/Stats.csv"); 

    XSSFWorkbook wb = new XSSFWorkbook(); 
    XSSFSheet ws = wb.createSheet("WriteToXL"); 

    // Iterate over webtable header row and header cells. 
    for(int r2=0; r2<irow2.size(); r2++) { 
    WebElement webRow2 = irow2.get(r2); 
     System.out.println(webRow2.getText()); 
     XSSFRow row2 = ws.createRow(r2); 

     List<WebElement> allCells2 = webRow2.findElements(By.tagName("th")); 

     for(int c2=0; c2<allCells2.size(); c2++) { 
      WebElement webCell = allCells2.get(c2); 
      String text2 = webCell.getText(); 
      XSSFCell excelCell2 = row2.createCell(c2); 
      excelCell2.setCellValue(text2); 

     } 
     System.out.println(""); 
     fos.flush(); 
     wb.write(fos); 
    } 

    // Iterate over webtable rows and cells. 
    for(int r=0; r<iRowCount; r++) { 
     WebElement webRow = irow.get(r); 
     System.out.println(webRow.getText()); 
     XSSFRow row = ws.createRow(r); 
     List<WebElement> allCells = webRow.findElements(By.tagName("td")); 

     for(int c=0; c<allCells.size(); c++) { 
      WebElement webCell = allCells.get(c); 
      String text = webCell.getText(); 

      XSSFCell excelCell = row.createCell(c); 
      excelCell.setCellValue(text); 
     } 

     System.out.println(""); 
    } 

    fos.flush(); 
    wb.write(fos); 
    fos.close(); 

    end(); 

} 
public static void end() { 
    driver.close(); 
    driver.quit(); 
} 

} 
+1

如果你正在寫只是一個CSV文件,然後使用Apache的POI做它是一個巨大的** **矯枉過正!這只是一個純文本文件。 – SiKing

+0

感謝您的建議,我最初選擇poi是因爲我試圖在xlsx文件中獲取輸出,但是每次運行腳本時都不會打開該文件。 研究表示poi行爲就像xlsx,而應該使用csv。 根據您的觀察,我會嘗試使用緩衝式閱讀器來做到這一點。我認爲這就是你的意思。非常感謝您的幫助。 – H1b1

回答

2

變化

webRow.findElements(By.tagName("td")) 

webRow.findElements(By.xpath("//td | //th")) 
+1

謝謝非常非常喜歡魅力。 – H1b1