You may notice the potential pitfalls mentioned above. For one, it may loop indefinitely because there is no control in case the element is forever stale. And, we’ll still need to repeat this code for additional actions like .GetAttribute, .IsEnabled, .Text which will create our scalability issue.If you go back to the definition of stale element reference error in MDN, you will notice it mentions a UUID, which the WebDriver uses to identify each element it fetches. This UUID changes whenever an element changes. When an element goes from not stale to stale back to not stale again, change is happening, right? Understanding this is crucial in the development of a more scalable solution to avoiding the stale element reference error. First, you will need to make sure you have access to that UUID in the framework you are using. In Selenium.NET, it is available through the ToString() method of the IWebElement interface. Then, you must find out which action triggers the replacement of the element you are trying to interact with. This step is critical because it defines a before and after and it will help you understand the application better, which is a win for you as part of the QA team. Now that you have identified the turning point, save the UUID of the element before that command.
Finally, you must find a way to limit the evaluation of this UUID. You can choose to limit by amount of iterations or time. Since the changes of the element happen over time, maybe time is the best way to go. In Selenium.NET, the WebDriverWait class will help since you can specify a timeout limit.
This solves both our pitfalls because it has a limit and a timeout exception. After the implementation of these lines, you are safe to interact with the element. Is it perfect? Maybe not, there still may be some scalability issues, but most of those can be easily addressed regardless of your architecture.
Provide your information to talk with a number8 Relationship Manager about your development needs today and feel what it’s like to be listened to before being sold a solution.
number8’s onshore office is located in Louisville, Kentucky where our Account and Relationship Managers work hard to provide all of our clients with exceptional customer service. We also have consultant offices located in Escazú, Costa Rica and San Pedro Sula, Honduras that give us a strong local presence allowing for top-level recruitment, technical training and low employee turnover.Our Locations
Got any questions? You can chat with us here: