-1

I have table with buttons. Each row of table consist of 3 buttons. I have to click on one of those 3 and then go to other page to see changes. After i assert that changes are good I want go back to those buttons and set them to it initial state. So in list I store initial state of those buttons. But when I go to the other page to see changes and go back to set those buttons to initial state. I get StaleElementReferenceException. I get why but even If I initialize elements again I get the same error. I use page object pattern and initialize elements in that way PageFactory.initElements(new ElementDecorator(Driver.getInstance()), this); Even if I put Thread.sleep and then initialize elements steel I get the error.

slovvic
  • 417
  • 3
  • 6
  • 14
  • Possible duplicate of [How to avoid "StaleElementReferenceException" in Selenium?](http://stackoverflow.com/questions/12967541/how-to-avoid-staleelementreferenceexception-in-selenium) – JeffC May 05 '17 at 16:00
  • Please read [ask] and [How much research effort is expected?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) Please provide the code you have tried and the execution result including any error messages, etc. Also provide a link to the page and/or the relevant HTML. – JeffC May 05 '17 at 16:00

1 Answers1

0

Don't use thread.sleep, use the below method which will return boolean elementPresent as true when element is found and false when the element is missing for some reason (or your xpath might not be good enough). Timeout is how long to wait before you can say that the element is not there (i.e 5 will wait for 5 seconds and then throw Element not found exception)

public boolean waitForElement(String elementXpath, int timeOut) {

    try{                    
    WebDriverWait wait = new WebDriverWait(driver, timeOut); 
    boolean elementPresent=wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(elementXpath)).isDisplayed());

    System.out.printf("%nElement is present [T/F]..? ")+elementPresent;
    }        
    catch(TimeoutException e1){e1.printStackTrace();elementPresent=false;}          

    return elementPresent;   
 }

also another recommendation when you go back to your tables could be to refresh the page before trying to locate any elements:

  driver.navigate().refresh();
Xwris Stoixeia
  • 1,831
  • 21
  • 22