1

I've been hitting my head against the wall trying to understand this and search Youtube, MSDN and this site but I can't move further with my scripts because of the StaleElementReferenceException. Here is the code:

//Opens the campaign gallery and Requests the template
driver.Url = "WEBSITE URL";


driver.FindElement(By.XPath("//input[@id='ctl00_ctl00_ctl00_Main_Main_Main_SearchTextBox']")).Click();

element.SendKeys("DPHR");
driver.FindElement(By.Id("ctl00_ctl00_ctl00_Main_Main_Main_SearchButton")).Click();
driver.FindElement(By.Id("ctl00_ctl00_ctl00_Main_Main_Main_rptCampaignTemplates_ctl00_gi_lnkGalleryRequest")).Click();

//Walks through the process of requesting the template and pushing it to pending status
driver.FindElement(By.Id("lnkGetStarted")).Click();
driver.FindElement(By.XPath("//form/div[9]/div/div[2]/div/div[2]/div[1]/div[3]/a/i")).Click();

Basically what is happening is I'm going through a gallery, and I'm trying to utilize the search box and enter text. But the exception gets thrown at

element.SendKeys("DPHR")

From what I can understand, it could be because once I click in the text box, the element refreshes and that error is thrown because its an "old" element. But I can't find any resource on how to circumvent this. Any insight is greatly appreciated!

5

StaleElementReferenceException is really common with Selenium.

To avoid these issues you should be waiting for an element to load before manipulating it (clicking, sending keys, etc). This will reduce the likelihood of something between dependent actions like your click and send keys. I will typically do an explicit wait on a single or multiple related element(s) (with a defined timeout period and a rescue clause in case of timeout) before attempting any action.

Explicit waits (waiting until a specific element has loaded) is typically the way to go. Although to be fair I haven't used C# and don't know if this is the case for every language. Implicit waits (waiting for a define period of time) is less good practice. For a good overview of Explicit vs Implicit waits, go here.

Just searching Google I've found a few references that might have some code examples to help you think through adding waits, prior to your click events:

  • 1
    This is the correct answer. I wanted to add that there may arise a small subset of cases where you will bump into StaleElementReferenceExceptions even after you have performed an explicit wait. In specific cases, it may be a good idea to simply catch the exception and re-locate the control to ensure you are working with a fresh element from that point forward. This should only be used if every other measure is taken to avoid the exception, and if it is not feasible to determine at run time whether or not the element is stale before attempting to interact with it. – VanderLinden Feb 17 '17 at 23:38
1

This occurs usually if you refresh your page which contains the element , re-defining / re- initializing the element after the refresh solves the issue.

0

Thank you all for your help, I found the answer in stackoverflow.

For those that were having the same issue, here is what I did:

utilize WebDriverWait

> WebDriverWait wait = new WebDriverWait(driver,TimeSpan.FromSeconds(3));

To do this, you would have to install the selenium support package and add

using OpenQA.Selenium.Support.UI;

Then, when you're running into the stale exception, before you attempt to click/add text/etc add the following code:

wait.Until(ExpectedConditions.ElementExists((By.Id("[ENTER ID OF ELEMENT]"))));

Basically what this does is searches for the element you're attempting to interact with, and once it appears in a usable state, the rest of your code will continue to be executed.

  • Did you get this from one of the links I posted? Like I said above it's good practice to wait until an element exists. Glad you solved the problem! – Chris Kenst Feb 16 '17 at 21:53
0

Try using implicit wait in Selenium rather than Thread.sleep, if you are providing condition till when it has to wait then you can use explicit wait.

Provide a wait and if it's still not working out contact your developer, probably he can help you out after walking you through the code base about how exactly the functionality works.

For more information on above provided Exception please refer here.

Its mostly due to element not being found.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.