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

+1 vote

I’m getting an error when using selenium for my app which is built in C#.

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

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

e.Click();

The error line is in the e.Click() but this was working fine earlier by using same XPath but failed on the last try. How can I fix it?

Jul 9, 2018 in Selenium by Martin
• 4,220 points
5,841 views
I am lucky to find this site! Interesting.
Hi Davidrania, welcome to the Community. It's like a one-stop place for all your questions. You can ask questions related to any technologies and the response time is really good.

2 answers to this question.

0 votes

It means that either the element changed in the page, or element gets deleted.You should try again, like this:

bool staleElement = true; 

while(staleElement){

  try{

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

     staleElement = false;


  } catch(StaleElementReferenceException e){

    staleElement = true;

  }

}

answered Jul 9, 2018 by Samarpit
• 5,130 points
+1 vote
go to your code and apply implicit wait with the Web Driver.

Thanks
answered Dec 21, 2018 by Malik
Hey @Malik it doesn't help.

Related Questions In Selenium

0 votes
2 answers
0 votes
1 answer

To check that the web page is loaded or not using Selenium Web Driver?

The solution is using Implicit Wait which ...READ MORE

answered May 24, 2018 in Selenium by Meci Matt
• 9,420 points
5,897 views
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,110 points
915 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,102 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
833 views