-3

This is giving

stale element reference: element is not attached to the page document

Cant I call objects of actions class multiple times? If I create a new object then it works fine

public void  actionMethods() throws Exception 
{
    Actions act = new Actions(driver);      
    WebElement ele1 = driver.findElement(By.xpath("//*[@id=\"menu-item-37\"]/a"));
    act.moveToElement(ele1).build().perform();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//*[@id=\"menu-item-4868\"]/a")).click();
    Thread.sleep(2000);

    driver.navigate().back();
    driver.navigate().refresh();
    Thread.sleep(2000);

    act.moveToElement(ele1).build().perform();
    Thread.sleep(2000);
    driver.findElement(By.xpath("//*[@id=\"menu-item-4877\"]/a")).click();

    Thread.sleep(3000);
}
Guy
  • 46,488
  • 10
  • 44
  • 88

1 Answers1

0

When you move to different page or even refresh the current page the driver "losses" all the elements it previously located.

ele1 is stale after the page navigation, you need to relocate it before you use it in the second act.moveToElement(ele1).

driver.navigate().back();
driver.navigate().refresh();
Thread.sleep(2000);

// find ele1 again
ele1 = driver.findElement(By.xpath("//*[@id=\"menu-item-37\"]/a"));
act.moveToElement(ele1).build().perform();
yong
  • 13,357
  • 1
  • 16
  • 27
Guy
  • 46,488
  • 10
  • 44
  • 88