I have 5 elements which I am tryin to hover 1 by 1. I am putting these elements in a List. But when I use List for Hover(with and without loop) it gives Stale Element Exception. Note I can perform it when I use individual elements without using list. I have printed the elements from List and the xpath returned also looks correct.
System.out.println("=====================All star start ================");
Actions actions= new Actions(driver);
List<WebElement> starList= new ArrayList<WebElement>();
ArrayList<WebElement> starColorList= new ArrayList<WebElement>();
Reporter.log("Started adding star elements", true);
for(i=11,j=22;i<=15;i ,j=j 2)
{
Reporter.log("Started adding star elements" i j, true);
WebElement s = driver.findElement(By.xpath("(//*[name()='svg'][@class='rvs-star-svg'])[" i "]"));
WebElement sc = driver.findElement(By.xpath("(//*[name()='svg']//*[name()='path'])[" j "]"));
starList.add(s);
starColorList.add(sc);
}
//Does not work even without loop
actions.moveToElement(starList.get(2)).build().perform();
//Works actions.moveToElement(star1).build().perform();
Does not work for(i=0;i<starList.size();i ) {
Reporter.log("Started Hover" i, true);
actions.moveToElement(starList.get(i)).build().perform();
Thread.sleep(2000);
System.out.println(starList.get(i));
//System.out.println("Star" i "has color " starColorList.get(i).getAttribute("stroke"));
}
'''
CodePudding user response:
There are several problems to watch out for:
StateElementReferenceException
is always thrown when you try to interact with an element which is no longer accessible because the DOM has changed. Are you trying to interact with the second element after you hovered over the first one? Does this cause a new element, like a tooltip, to show up and thus invalidating the WebElements from the list? You'd have to re-execute the search after that. One basic solution is to keep locators, and not located WebElements in the list, so that you runfindElement(locators.get(i)).click()
in a loop.This not necessarily matches your example, but it seems to be quite common problem: if you use
findElements
- the variant returning a list of multiple elements matching a single locator - make sure your locator is not too generic, as sometimes the found elements are not necessarily what you are expecting them to be. Like finding nested<td>
elements inside other<td>
and not after the ones you'd expect.
All in all, the list use here is probably a red herring - the real problem is what happens in the DOM between calls to findElement()
and whatever interaction you have with that element.
CodePudding user response:
StateElementReferenceException will occur on when trying to access the element before page load or after the element is vanished. so please try to add visibility wait conditions.
By tmpBy = By.xpath("//div[contains(text(),'" deno "')]");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(tmpBy));
wait.until(ExpectedConditions.visibilityOfElementLocated(tmpBy));
driver.findElement(tmpBy).click();
And please add code to take screenshot whenever it is failing. it will be easy for you debug.