I have this problem in my code with Stale Element exception, where my script, which is suppose to gather certain elements into an excel file, fails in one particular spot when I try to run it normally, but if I place a breakpoint to that place and run past it in a debug mode, it finishes properly:
public void gatherDataAndCreateExcelFile(String fileName) throws InterruptedException, IOException {
ArrayList<String> key = new ArrayList<String>();
ArrayList<String> summary = new ArrayList<String>();
ArrayList<String> assignee = new ArrayList<String>();
List<WebElement> dataTable;
do {
// extract the data
gatherData(key, summary, assignee);
// look, if pagination is displayed and if there is another page to move to. Then continue extracting the data there
if (findElementByXpath("//*[@class='pagination']").isDisplayed()) {
if (findElementByXpath("//*[@class='pagination']/a[(@class='nav-next')]").isDisplayed()) {
findElementByXpath("//*[@class='pagination']/a[(@class='nav-next')]").click();
// extract the data - put a breakpoint to the line below
gatherData(key, summary, assignee);
}
}
}
while (checkIfNextPaginationPageExists());
createExcelTable(fileName, key, summary, assignee);
}
public void gatherData(ArrayList key, ArrayList summary, ArrayList assignee) throws InterruptedException {
List<WebElement> dataTable = findElementsByXpath("//*[@id='issuetable']/tbody/tr");
for (WebElement dataitem : dataTable) {
key.add(findElementByXpath(dataitem, "./td[1]/a[1]").getAttribute("title"));
summary.add(findElementByXpath(dataitem, "./td[3]/p/a").getText());
try {
assignee.add(findElementByXpath(dataitem, "./td[10]/span/a").getAttribute("id"));
} catch (NoSuchElementException e) {
assignee.add("Unassigned");
}
}
}
If I put breakpoint on the other occurrence of gatherData method, run it in a debug mode, press “step over/into” thrice to move inside the for loop, then it finishes successfully, but during the normal run, it always gives that exception.
- I know what the exception means, but at that point I am no longer actively modifying the web page, so I suppose there is some loading on the background going on
- when I tried to print out the output, I noticed one thing – that before it crashes, it once more prints out first result of the data it found during the first occurrence of gatherData method in gatherDataAndCreateExcelFile (let´s say, there are 50 items in one page and 20 on the other, once it successfully mules over the first 50 and correctly lists the remaining 20 in the first line of gatherData, then in the following for loop it still prints out the first result out of the first 50 and then crashes), as if it didn´t had time to “refresh” the data – though allowing it to wait any amount of time before the for loop inside gatherData does not help – it always crashes regardless. But when I put the breakpoint and do what I described in a previous paragraph, it works.
Does anybody know what might be wrong with my code? Maybe I am missing something obvious and any help would be appreciated.