Webdriver脚本不会打印到文本文件

时间:2016-03-05 16:08:30

标签: java selenium-webdriver

问题: 我的Java WebDriver脚本正在创建文本文件,正确地将所有内容打印到控制台,但不会打印到所述文本文件。该文件始终为空白。

我的观察: 它与我如何编写缓冲编写器write()和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();
    }
}

1 个答案:

答案 0 :(得分:1)

问题在于每次调用代码时

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

它正在重写文件,而不是添加一行。

你正在为找到的每个元素调用它,所以只有最后一个值保存在文件中,该文件恰好是空的。

我建议您首先构建要存储在文件中的String,然后将其写入文件。像这样:

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();
    }
}