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,340 points
21,583 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.

+1 vote

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,890 points
Thanks......I resolved the issue.
thanks bro its working for me.
Hi Please tell how to implement this solution in protractor
Hey, @Rahul I'd recommend you using browser.wait and Expected Conditions to solve this issue.
Perfect. Thanks. it's worked for me.
Great!!! worked for me... :)
+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

+1 vote
0 answers

python selenium error: element is not attached to the page document

I am scraping Banggood, the problem is ...READ MORE

Apr 9 in Selenium by eslam
• 130 points
2,481 views
+1 vote
2 answers
0 votes
2 answers

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,520 points
3,752 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,736 views
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,890 points
2,726 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
3,078 views
0 votes
1 answer

How to scroll until the element is in view using Selenium2Library Keyword?

You have to download the ExtendedSelenium2Library from ...READ MORE

answered Jun 25, 2018 in Selenium by Samarpit
• 5,890 points
5,262 views
0 votes
1 answer

Selenium-Debugging Error: Element is not clickable at point (X,Y)

Another element is covering the element you ...READ MORE

answered Jul 2, 2018 in Selenium by Samarpit
• 5,890 points
5,709 views