Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+7 votes
2 views
in DevOps and Agile by (47.6k points)

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 the same XPath but failed on the last try. How can I fix it?

1 Answer

+7 votes
by (106k points)

If you are using Selenium in your app and you are getting means either the element changed in the page, or element gets deleted. So you can use the below-mentioned code to get rid of the error:

bool staleElement = true; 

while(staleElement){

  try{

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

     staleElement = false;

  } catch(StaleElementReferenceException e){

    staleElement = true;

  }

} 

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...