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();
}
}
谢谢安德鲁。 – H1b1