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,330 points
15,238 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,870 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.
+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
387 views
+1 vote
2 answers
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,510 points
2,642 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,870 points
2,078 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,870 points
2,195 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,870 points
4,292 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,870 points
4,600 views