0

Hello i have to save from a page same <a> in the page and has got as class=my_img i save this elements in a List an after i try to go in firts element of list and after go back get the second element but the selenium give me this error

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up

and this is my code

    List <WebElement> Element = drivers.findElements(By.cssSelector(".my_img"));
    System.out.println("Megethos"+Element.size());
    System.out.println("Pame stous epomenous \n");
    for (i = 1; i < Element.size(); i++) {
        drivers.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
        System.out.println(i+" "+Element.size());
        System.out.println(i+" "+Element.get(i));
        action.click(Element.get(i)).perform();
        Thread.sleep(2000);
        System.out.println("go back");
        drivers.navigate().back();
        Thread.sleep(6000);
        drivers.navigate().refresh();
        Thread.sleep(6000);

    }
Vagos
  • 127
  • 1
  • 2
  • 9

2 Answers2

2

Your action.click() and/or navigate() calls are resulting in a page reload, causing the WebElement's in your list to no longer be valid. Put your findElements() call inside the loop:

List <WebElement> Element = drivers.findElements(By.cssSelector(".my_img"));
for (i = 1; i < Element.size(); i++) {
    Element = drivers.findElements(By.cssSelector(".my_img"));
    drivers.manage().timeouts().implicitlyWait(35, TimeUnit.SECONDS);
    System.out.println(i+" "+Element.size());
    System.out.println(i+" "+Element.get(i));
    action.click(Element.get(i)).perform();
    Thread.sleep(2000);
    System.out.println("go back");
    drivers.navigate().back();
    Thread.sleep(6000);
    drivers.navigate().refresh();
    Thread.sleep(6000);

}
Don Bottstein
  • 1,640
  • 13
  • 17
  • 1
    This is correct answer however depending on the functionality of the application click on the element can be different – Saifur Dec 10 '14 at 20:35
1

If the primary purpose is to click on the links and get back to the previous page, its better to get "href" attributes for all the "a" elements in the page and navigate to each of them. The way you've followed will always result in StaleElementReferenceExeception, as when you navigate back to the original DOM changes.

Below is the way like I suggested:

List<WebElement> linkElements = driver.findElements(By.xpath("//a[@class='my_img']"));
System.out.println("The number of links under URL is: "+linkElements.size());

//Getting all the 'href' attributes from the 'a' tag and putting into the String array linkhrefs
String[] linkhrefs = new String[linkElements.size()];
int j = 0;
for (WebElement e : linkElements) {
    linkhrefs[j] = e.getAttribute("href");
    j++;
}

// test each link
int k=0;
for (String t : linkhrefs) {
    try{
        if (t != null && !t.isEmpty()) {
            System.out.println("Navigating to link number "+(++k)+": '"+t+"'");
            driver.navigate().to(t);
            String title;
            title = driver.getTitle();
            System.out.println("title is: "+title);

            //Some known errors, if and when, found in the navigated to page.
            if((title.contains("You are not authorized to view this page"))||(title.contains("Page not found"))
                            ||(title.contains("503 Service Unavailable"))
                            ||(title.contains("Problem loading page")))
            {
            System.err.println(t + " the link is not working because title is: "+title);
            } else {
                System.out.println("\"" + t + "\"" + " is working.");
            }
        }else{
            System.err.println("Link's href is null.");
        }
    }catch(Throwable e){

        System.err.println("Error came while navigating to link: "+t+". Error message: "+e.getMessage());
    }

    System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
Subh
  • 4,354
  • 1
  • 13
  • 32