Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what this error mean: stale element reference: element is not attached to the page document?

In my C# app to use selenium web driver I get this error:

OpenQA.Selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document

in this code:

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

the error line is in the e.Click() but this is a procedure that executed successfully in same link specified by XPath before but failed on the last try! so what this error mean and how to fix it ?

like image 586
zac Avatar asked Apr 21 '17 13:04

zac


People also ask

How did you overcome stale element exception?

Solution 1: (Handle it using POM "@FindBy") If we use Page Object Model (or) Page Object design pattern you will never get Stale Element Reference Exception.


2 Answers

It means that either the element changed in the page, or element gets deleted, full reference in this link http://www.seleniumhq.org/exceptions/stale_element_reference.jsp

One way to cope with this, you could put retry, probably something like

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

  } catch(StaleElementReferenceException e){
    staleElement = true;
  }
}
like image 94
adee alamsz Avatar answered Sep 29 '22 23:09

adee alamsz


I'd the same problem when i was doing date picker, with one of the site. I'd to get all the active (or enabled) buttons from the date picker and click each of that. When I iterated the elements it was getting stale. I'd re reiterate keeping another list. This may have happened because once the List gets selenium does not refer it back. 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);

    }

}
like image 38
Naveen Avatar answered Sep 29 '22 23:09

Naveen