0

I ran into this weird situation.

  1. I am first at page 1, which has an element with id of "abc", I use "abc" to find webElement and get its text value

  2. I click on a link in page 1, it takes me to page 2

  3. In page 2, there is also an element with id of "abc", when i try to use "abc" to find element and get its text value, webdriver gives me a "stale element exception, element not attached to DOM etc"

I google to get to this page, http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp. It explains that "is that page that the element was part of has been refreshed, or the user has navigated away to another page... the driver has no way to determine that the replacements are actually what's expected"

So how to solve this kind of question? In theory, there is no way for webdriver to know that these elements are in two different pages.

It's worth to notice that if i insert a hard coded delay (a thread sleep etc) in between the page switch, the stale issue will not appear.

Thanks,

user1559625
  • 2,583
  • 5
  • 37
  • 75
  • 1
    it will be of help if you will post your sample code for the above – Rajnish Kumar May 19 '16 at 13:36
  • also your question is little confusing are you looking how to get rid of stale element exception or you want to know how selenium will know we have two different pages with two different element with same locator strategy. – Rajnish Kumar May 19 '16 at 13:42
  • you might be using a static variable[webelement] in the method that you would use to find the element... – TechDog May 19 '16 at 14:02

2 Answers2

0

hi please do it like below if you want to get rid of Stale element exception

// Case one - when you are simply calling firstPageText two time
// then before calling it on the next page please re identify it 
// to get rid of stale element exception.

   String firstPageText =  driver.findElement(By.id("abc")).getText();
   System.out.println("Text on the first page is : " + firstPageText);

   driver.findElement(By.linkText("to second page ")).click();

   String secondPageText =  driver.findElement(By.id("abc")).getText();
   System.out.println("Text on the Second page is : " + secondPageText );

also if u have created a method which performs the above scenario then to avoid above error please make two separate methods one for page 1 and one for page two both having its individual identification strategy.

Rajnish Kumar
  • 2,828
  • 5
  • 25
  • 39
0

A stale element is an element that has been removed from the page. So in your case, you are probably getting the element just before the document is updated. To avoid this situation, you could first wait for the element to become stale before getting the new element:

// get the element
WebElement element = driver.findElement(By.id("abc"));

// trigger the update of the document
driver.findElement(By.id("my-button")).click();

// wait for the element to become stale
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.stalenessOf(element));

// get the new element
WebElement element = driver.findElement(By.id("abc"));
Florent B.
  • 41,537
  • 7
  • 86
  • 101
  • B.In my case, the page is loading slow, so i do not think it is necessary (and i do not have an another element to click to trigger the update of the document). So i'll try to use your way to wait for staleness ONLY. Thanks. – user1559625 May 19 '16 at 22:02
  • Btw, my previous experience is that by using sth like wait.until.element.clickable will always solve the staleness problem. From your code sample i think maybe the wait.clickable use a trial click internally. However, this time i was trying to GET THE VALUE of element, and I WAS using wait.until.element.present, and it will not trigger the update of document. Could this be the reason? – user1559625 May 19 '16 at 22:09
  • The `element_to_be_clickable` checks `element.is_enabled()` and `element.is_displayed()`. This example handles the case where the element is at some point removed and replaced by another reference. Anyway without having your code and without knowing the behaviour of the page, there's nothing much I can do. – Florent B. May 19 '16 at 22:44