0

I am working on clicking on the navigation links(marked as 1,2,...Next) for a particular search in the site dice.com

When I run the below mentioned code, it is executed once and then displays StaleElementReferenceException

Request you to help in resolving this issue

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Ex2 {  
public static void main(String[] args) {

    WebDriver driver=new FirefoxDriver();
    driver.get("http://dice.com");
       driver.findElement(By.xpath("//input[@id='FREE_TEXT']")).sendKeys("Selenium");
    driver. findElement(By.xpath("//*[@id='searchSubmit']")).click();

    //block that has navigation links
    WebElement b=driver.findElement(By.xpath("//*[@id='yui-main']/div/div[1]/div[1]/div[1][@class='pageProg']"));

              //navigation links
    List<WebElement> allLinks=b.findElements(By.tagName("a"));
    System.out.println("Total links -->" + allLinks.size());    


    for(int i=0;i<allLinks.size();i++){

        allLinks.get(i).click();
        Thread.sleep(5000);
    }

}

}

The error displayed is

Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: 59 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/stale_element_reference.html Build info: version: '2.35.0', revision: '8df0c6b', time: '2013-08-12 15:43:19' System info: os.name: 'Windows 8', os.arch: 'amd64', os.version: '6.2', java.version: '1.7.0_11' Session ID: 0410f597-c149-46b5-a2b7-e84c61cc73f1

2 Answers 2

1

The issue is when you click the first link, the page is reloaded, and the reference Selenium has to the page becomes stale. I think this approach will work for you instead:

List<WebElement> allLinks=b.findElements(By.tagName("a"));
System.out.println("Total links -->" + allLinks.size());    

String[] linkText = new String[allLinks.size()];

for(int i=0;i<allLinks.size();i++)
{
    linkText[i] = allLinks.get(i).text;
}

for(int i=0;i<linkText.length;i++)
{
    findElements(By.linktext(linkText).click();
    Thread.sleep(5000);
}
Sign up to request clarification or add additional context in comments.

Comments

0

use this as a work around

try{
//Your code which causes exception

}
catch(org.openqa.selenium.StaleElementReferenceException e){
//Repeat the code in try
}

Reason for exception is because javascript has loaded the element one more time with same name or id or whatever and you are still referring to element which is not present now.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.