I am working for a mouse hover and i want to test all the links working condition by clicking each and every link using for loop.In my program the iteration is going once and fo
You can use following modification in your code:-
List<WebElement> links=driver.findElements(By.xpath("//a[contains(@class,'sf-depth-2')]"));
for(int i=0;i<links.size();i++)
{
List<WebElement> allLinks=driver.findElements(By.xpath("//a[contains(@class,'sf-depth-2')]"));
allLinks.get(i).click();
driver.navigate().back();
}
Understand one fundamental thing: staleElementReferenceException
, by it's name suggests that the reference to your elements on the page is stale (lost). You just have to refer to those elements again, if they are available, which is apt in this case as the page has been refreshed. Do 2 things:
I'm not gonna give you the code directly:)Try it and come back with your code.
Refer here for it's clear explanation of possible scenarios for this particular exception
One more thing: Your for loop gives IndexOutOfBoundsException
. Modify that as well