Selenium – StaleElementReferenceException

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật