1

This code will iterate through a number of pages to find and extract elements on the page. Once the loop has completed it will generate a log file with these elements from a HashMap, but the results aren't being appended and are rather being overwritten.

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

Any ideas?

Mohsin Awan
  • 1,176
  • 2
  • 12
  • 29
Nazrod12
  • 111
  • 1
  • 2
  • 9

2 Answers2

1

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

If there is any entry in HashMap with the same text as element1.getText(), it will override it.

Also you are creating map for each call, it will create a new Map each time and lead to data loss for earlier content.

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

You should create this at instance level.

For generating unique key, define a number variable at instance level and increment that for each put.

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

May be change the HashMap to take long as Key instead of String.

ManishKr
  • 211
  • 2
  • 9
  • Okay, how can I get around that? – Nazrod12 Dec 01 '16 at 09:54
  • How would you solve this? – Nazrod12 Dec 01 '16 at 09:59
  • two things, don't initialize map in each call otherwise old data will be lost. Secondly to generate unique key you may take any int/long type instance variable and increment that after each put call. – ManishKr Dec 01 '16 at 10:00
  • I moved that to instance level and am now getting: org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"} which is strange because the URL was working fine before – Nazrod12 Dec 01 '16 at 10:33
  • just moving map to instance level can't lead to this exception in any way :) – ManishKr Dec 01 '16 at 11:01
  • If I put it back then the URL works fine :S – Nazrod12 Dec 01 '16 at 11:08
  • curious to know if you are moving this statement this.map = new HashMap(); to instance level, like this HashMap map = new HashMap(); or something else? – ManishKr Dec 01 '16 at 13:51
  • 1
    It was something wrong in the getAuthors() method, it's all fixed now. Thanks for your help, your solution worked fine – Nazrod12 Dec 01 '16 at 14:02
0
  for (WebElement element1 : allElements) {
i++
        map.put(element1.getText()+i, element1.findElement(By.tagName("a")).getAttribute("href"));

    }

add i++ so it doesnt override

MNJ
  • 172
  • 1
  • 1
  • 13