【问题标题】:Appending to text file [duplicate]附加到文本文件[重复]
【发布时间】:2016-12-01 09:50:34
【问题描述】:

此代码将遍历多个页面以查找和提取页面上的元素。循环完成后,它将从 HashMap 生成包含这些元素的日志文件,但结果不会被追加,而是被覆盖。

        int d = new Integer(0);
        for (int i = 0; i <= 100; d += 10) {
            String url = Constants.FilterUrl + "&startIndex=" + d;
            this.getAuthors();
            driver.get(url);
            if (!driver.getPageSource().contains("h3")) break;
            }   

        /* Send HashMap values to text file */
        File file = new File(Constants.FILEPATH + Constants.dateFormat.format(new Date()) + ".txt");

        try{
            if(!file.exists()){

                System.out.println("We had to make a new file.");
                file.createNewFile();
            }
                PrintWriter out = new PrintWriter(new FileWriter(file), true);
                map.forEach((k, v) -> out.println(k + ", " + v));
                out.append("************** " + "\n");
                out.close(); 
            } catch(IOException e) {
                System.out.println("COULD NOT LOG!!");
            }
}

public void getAuthors(){
    List<WebElement> allElements = driver.findElements(By.tagName("h3"));
    /* Create HashMap and store H3 elements in the key set */
    this.map = new HashMap<String, String>();
    for (WebElement element1 : allElements) {
        map.put(element1.getText(), element1.findElement(By.tagName("a")).getAttribute("href"));

    }

    /* Visit pages for H3 elements and retrieve names of the authors */
    for (Map.Entry<String, String> entry : map.entrySet()) {
        driver.get(entry.getValue());
        entry.setValue(driver.findElement(By.className("userlink-0")).getText());
    }
}

有什么想法吗?

【问题讨论】:

    标签: java selenium selenium-webdriver automated-tests


    【解决方案1】:

    map.put(element1.getText(), element1.findElement(By.tagName("a")).getAttribute("href"));

    如果 HashMap 中有任何与element1.getText() 相同文本的条目,它将覆盖它。

    此外,您正在为每次调用创建地图,它每次都会创建一个新地图并导致早期内容的数据丢失。

    /* Create HashMap and store H3 elements in the key set */
    this.map = new HashMap<String, String>();
    

    您应该在实例级别创建它。

    为了生成唯一键,在实例级别定义一个数字变量并为每个 put 递增。

    long counter = 0;
     map.put(counter++, element1.findElement(By.tagName("a")).getAttribute("href"));
    

    可能将 HashMap 更改为 Key 而不是 String。

    【讨论】:

    • 好的,我该如何解决?
    • 你会如何解决这个问题?
    • 两件事,不要在每次调用中初始化地图,否则旧数据会丢失。其次,要生成唯一键,您可以使用任何 int/long 类型的实例变量,并在每次 put 调用后递增。
    • 我将其移至实例级别,现在得到:org.openqa.selenium.WebDriverException:未知错误:未处理的检查器错误:{“code”:-32603,“message”:“无法导航到无效的 URL"} 这很奇怪,因为该 URL 之前工作正常
    • getAuthors() 方法出了点问题,现在都已修复。感谢您的帮助,您的解决方案运行良好
    【解决方案2】:
      for (WebElement element1 : allElements) {
    i++
            map.put(element1.getText()+i, element1.findElement(By.tagName("a")).getAttribute("href"));
    
        }
    

    添加 i++ 使其不会覆盖

    【讨论】:

      猜你喜欢
      • 2016-03-25
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-11
      • 1970-01-01
      • 1970-01-01
      • 2015-07-02
      相关资源
      最近更新 更多