2016-03-05 68 views
0

Issue: 我的Java WebDriver腳本正在創建文本文件,將所有內容都正確打印到控制檯,但不會打印到所述文本文件。該文件始終爲空。Webdriver腳本不會打印到文本文件

我的觀察: 它有一些做我怎麼寫的緩衝作家寫()和close()函數,但我不能安靜的地方,我的手指上是一個菜鳥。所有的幫助是高度讚賞。

我的代碼:

public class WebTableToTxtFile { 
    static WebDriver driver = new FirefoxDriver(); 

    public static void main(String[] args) throws Throwable { 
     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()); 

     File txtFile = new File("MyFileLocation/Output.txt"); 

     for (int r = 0; r < irow.size(); r++) { 
      WebElement webRow = irow.get(r); 
      System.out.print(webRow.getText()); 
      List<WebElement> allCells = webRow.findElements(By.xpath("th | td")); 

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

       FileWriter fw = new FileWriter(txtFile.getAbsolutePath()); 
       BufferedWriter bw = new BufferedWriter(fw); 

       bw.write(text); 
       bw.close(); 
      } 
      System.out.println(""); 
     } 

     end(); 
    } 

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

謝謝安德魯。 – H1b1

回答

1

的問題是,每一次你在呼喚你的代碼

FileWriter fw = new FileWriter(txtFile.getAbsolutePath()); 
BufferedWriter bw = new BufferedWriter(fw); 
bw.write(text); 
bw.close(); 

它改寫文件,而不是添加一行。

而你正在爲每個找到的元素調用它,所以只有最後一個值保存在文件中,而這個文件恰好是空的。

我建議你先建立你想存儲在文件中的字符串,然後把它寫入文件。像這樣:

public class WebTableToTxtFile { 
    static WebDriver driver = new FirefoxDriver(); 

    public static void main(String[] args) throws Throwable { 
     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()); 

     File txtFile = new File 
       ("MyFileLocation/Output.txt"); 
     StringBuilder sb = new StringBuilder(); 

     for(int r=0; r<irow.size(); r++) { 
      WebElement webRow = irow.get(r); 
      System.out.print(webRow.getText()); 

      List<WebElement> allCells = webRow.findElements(By.xpath("th | td")); 

      for(int c=0; c<allCells.size(); c++) { 
       WebElement webCell = allCells.get(c); 
       String text = webCell.getText(); 
       System.out.print(text); 
       sb.append(text + '\n'); 
      } 

      System.out.println(""); 

     } 

     FileWriter fw = new FileWriter(txtFile.getAbsolutePath()); 
     BufferedWriter bw = new BufferedWriter(fw); 
     bw.write(sb.toString()); 
     bw.close(); 

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

感謝一羣拉里莎!像魅力一樣工作。 – H1b1