Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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!
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:
https://watirmelon.blog/2014/01/29/waiting-in-c-webdriver/
http://toolsqa.com/selenium-webdriver/c-sharp/advance-explicit-webdriver-waits-in-c/
https://stackoverflow.com/questions/6992993/selenium-c-sharp-webdriver-wait-until-element-is-present -> this answer looks like its recommending implicit waits.
–
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.
–
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.
Thanks for contributing an answer to Software Quality Assurance & Testing Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.