What does this error mean: deprecated element reference: element not attached to page document?

In my C # application for using selenium web driver I am getting this error:

OpenQA.Selenium.StaleElementReferenceException: deprecated reference element: element not attached to page document

in this code:

IWebElement e = driver.FindElement(By.XPath(link_click), 10);
e.Click();

      

the error string is in e.Click()

, but this is a procedure that succeeded in the same reference given in XPath before, but has not been executed on the last try! so what does this error mean and how to fix it?

+3


source to share


2 answers


This means that either the element changed in the page or the element is removed, full link in this link http://www.seleniumhq.org/exceptions/stale_element_reference.jsp

One way to deal with this, you can try again, maybe something like



bool staleElement = true; 
while(staleElement){
  try{
     driver.FindElement(By.XPath(link_click), 10).Click();
     staleElement = false;

  } catch(StaleElementReferenceException e){
    staleElement = true;
  }
}

      

+6


source


I would have the same problem when doing date picker with one of the sites. I have to get all active (or enabled) buttons from the date picker and click each one. When I repeated the elements, it became obsolete. I'll repeat, keep a different list. This may have happened because once the List receives selenium, it does not link to it. Below is the fixed code



@Test
public void datePickerTest() throws InterruptedException{
    driver.get(baseURL);
    // click on flights tab 
    genericMethod.getElement("tab-flight-tab-hp", "id").click();

    // click departing date, such that the date picker is loaded  in the dom 
    genericMethod.getElement("flight-departing-hp-flight", "id").click(); 
    Thread.sleep(700);

    // pass the collected xpath where you can find all the dates which are enabled 
    String xpath=".//*[@id='flight-departing-wrapper-hp-flight']/div/div/div[2]/table/tbody/tr/td/button[not(@disabled)]";
    List<WebElement> activeDatesWebElement = genericMethod.getElements("xpath", xpath); 

    System.out.println("Number of Active Dates " + activeDatesWebElement.size());

    // work around for an element when it is found stale 
    List<String> activeDateListAsString = new ArrayList<String>();

    for(WebElement temp : activeDatesWebElement){
        activeDateListAsString.add(temp.getText());
    }


    // iterate all element in the list received, which is kept in list 
    for(String temp : activeDateListAsString){
        genericMethod.getElement("flight-departing-hp-flight", "id").click();

        Thread.sleep(500);


        String selectDateXpath=".//*[@id='flight-departing-wrapper-hp-flight']"
                + "/div/div/div[2]/table/tbody/tr/td/button[text()='"
                +temp+"']";

        genericMethod.getElement(selectDateXpath, "xpath").click(); 
        Thread.sleep(500);

    }

}

      

+1


source







All Articles