The Stale Element Reference Exception is one of the infamous exceptions in selenium. This exception is caused when:. Every HTML webpage contains elements buttons, links, checkbox etc. In this example, we will try to perform some operation on a web element. Once the action is performed, we will refresh the browser through the code and try to perform same operation on the same web element.
A pop up is displayed with some text and Close button. Click on the Close button and then hit refresh to again see the pop up. This refresh operation triggers DOM restructuring and our test will fail if it tries to click on Close button again.
This failure happens due to Stale Element Reference Exception as the reference of Close button is obsolete after the page refresh. Below code tries to emulate the Stale Element Reference Exception:. Below is break down of the code:.
To start with the solution, consider following points:. We will write a new static method retryClickElement which accepts 2 arguments:. This method is declared statichence WebDriver variable driver reference is common to this method too. Below is the complete updated code:. The second restartModal. Using the above logic, our program will always send the xpath value instead of web element reference whenever there is a change in DOM. Your email address will not be published.
Save my name, email, and website in this browser for the next time I comment. Menu Selenium TestNG. When I am not working, I like to spend time with my family, cooking and learning new developments in IT.
Leave a Reply Cancel reply Your email address will not be published. Search for:. Recommended Reads. Subscribe Here!
How to solve Stale Element Reference exception in Selenium Webdriver
First Name.This Exception occurs when driver is trying to perform action on the element which is no longer exists or not valid. In order overcome this, we need to explicitly wait until the DOM is in a state where we are sure that DOM won't change. Here the reference to the element in the DOM that previously had becomes stale and we can no longer able to use this reference click call to interact with the element in the DOM.
Try to get around this by first using an explicit wait on the element to ensure the ajax call is complete, then get a reference to the element again. You can also user implicit waits, in which Webdriver will check for the element to become present until the specified timeout:. If even this is not working for you, try giving an other locator which gives some positive results.
And in the last if nothing works out, just retrying it for second time would work sometimes. This solution is not recommended because of time taken for each iteration that fails to find the element and end up wasting time in retry.
I trying to integrate webdriver with PhantomJSDriver for headless browser automation but its not working properly. Getting error. IllegalStateException: The path to the driver executable must be set by the phantomjs. I think you need to add the path of your phantom to your class path.
You need to open the environmental variable and update the path. Use ' try -- catch' in for loop for Java, ' try -- except' in for loop for Python. Hi I have one label after clicking on it it is dispalying list of image alnog with the iteam example its displying salad and asking user to select list of the salad displying in the list of the images ,dispalying pie and asking select list of the items size like pie likewiese the label and images are getting change after every click if user will select the label displyed list of images user will able to click on verify button and page will redirect to the next if not the label will refresh to new item along with list of the images.
I have tried explicit wait as well as the above code but still getting the same error : Error : "Element not found in the cache - perhaps the page has changed since it was looked up Command duration or timeout: It works some times but not always.
Is there any other way to handle this? Skip to main content.
How To Handle staleelementreferenceexception Selenium Webdriver
Check Our Demo Website! Selenium Easy. Free selenium tutorials for beginners and experts. Selenium Tutorials:.
Subscribe to RSS
Selenium Tutorials. HiI trying to integrate webdriver with PhantomJSDriver for headless browser automation but its not working properly. Regards, Dhanapalan. It depends on browser. Your name. More information about text formats. Save Preview.
All Rights Reserved.Really thanks for this best explanation. Even after iteration this solution is not working for me can some one suggest me a working solution. StaleObjectReference could be a real nightmare. I got the idea of StableWebElement - a wrapper which could detect Stale reference situation and try to find a new reference to the original element.
I tried with 2nd solution:' for loop' and my problem got solved. How To Handle staleelementreferenceexception Selenium Webdriver. One more webdriver exception Is stale element reference exception.
Just recently I have faced this exception In my test with exception message as bellow. StaleElementReferenceException: Element not found in the cache - perhaps the page has changed since it was looked up. Stale means old or we can say no longer fresh element. Let me describe you In very simple words. Example : You have a search text box on software web page.
Unknown 1 November at Unknown 2 January at I have seen so many exception in my application and after struggling couple of hours got the solution, which I already listed. You can refer below post, which will help you in case if you will get the exception. If you are new to Selenium then refer post to see how to handle exception. Please check Selenium official documentation for the same.
Sometimes it takes the time to attach element on Dom so you can retry using for loop and try catch. In the above program, we will try for specific element max 3 times and if the element is located in the first shot itself then it will break from for loop and come out of the loop. This solution, which worked for me, I have mentioned here if you have any additional scenario, which worked for you then comment below or contact me so that we can add into solution list.
Could you please create an example for this as for those who have not faced this problem might not be able to implement this in a program. Even if you can inform some URL where this can be tested it would be great.
Hi Mukesh, I am confused a little bit. Why using explicit or implicit wait is not one of the solutions? Wait does not work always. Explicit works sometime for this but implicit wait never works in this exception. Could you please suggest some solutions for this specific scenario or can we expect these failure since we are running around scripts.
Automation never gives ROI but still you can add below feature in your project which will run failed test cases. I am also getting the above exception while selecting value from drop down. Can i solve this by using the solution no 2?
Sometimes, my tests fail due to a StaleElementReferenceException. Could you suggest some approaches to making the tests more stable? This can happen if a DOM operation happening on the page is temporarily causing the element to be inaccessible. To allow for those cases, you can try to access the element several times in a loop before finally throwing an exception. Try this excellent solution from darrelgrainger. I was having this issue intermittently.
Get around this by first using an explicit wait on the element to ensure the update is complete, then grab a fresh reference to the element again. But anyway, best solution is to rely on Selenide library, it handles this kind of things and more.
The reason why the StaleElementReferenceException occurs has been laid out already: updates to the DOM between finding and doing something with the element. This actually waits and checks if the element in question has been refreshed during the specified timeout and additionally waits for the element to become clickable.
Have a look at the documentation for the refreshed method. In my project I introduced a notion of StableWebElement. It is a wrapper for WebElement which is able to detect if element is Stale and find a new reference to the original element. I have added a helper methods to locating elements which return StableWebElement instead of WebElement and the problem with StaleElementReference disappeared.
Kenny's solution is deprecated use this, i'm using actions class to double click but you can do anything. This is heavily based off of jspcal's answer but I had to modify that answer to get it working cleanly with our setup and so I wanted to add it here in case it's helpful to others.This means that motivation remains high to post winning tips.
You can apply to join the team as well and compete for the prizes yourself. There are many more recreational and promising punters that joined our team and share their football tips. Take your time to check them on the according pages. Please have in mind that they are also punters, with their good and their bad periods.
They spend time to conclude to a final pick, and this requires time and effort. Many times in the past, visitors told us that we are very strict in terms of rules. We require them to write detailed predictions and to justify their bets.
But think about it. In this way, the visitor is properly informed of all details of a match. We do not need football tips like "Bet on A to win". We are facing thousands of these with no valuable information every season. We are forced to remove them due to our different mindset. There are many sites that work this way.
Betshoot is not one of them. Bettors need to find value on their football tips. Note that we don't offer Horse racing tips, golf and e-sports. Cricket, snooker, boxing and darts are sometimes posted. The football accumulator section is about to be added soon. Everyone can comment and judge an article and its author. All free bets posted on Betshoot have a space for comments. Agree or disagree with someone.
Get in, express yourself.Since both of these scores are 20, the median is 20. If the two middle scores had different values, you would have to interpolate to determine the median.
The mode is the most frequently occurring value in the set of scores. To determine the mode, you might again order the scores as shown above, and then count each one. The most frequently occurring value is the mode.
In our example, the value 15 occurs three times and is the model. In some distributions there is more than one modal value. For instance, in a bimodal distribution there are two values that occur most frequently.
Notice that for the same set of 8 scores we got three different values -- 20. If the distribution is truly normal (i. Dispersion refers to the spread of the values around the central tendency. There are two common measures of dispersion, the range and the standard deviation.
The range is simply the highest value minus the lowest value. The Standard Deviation is a more accurate and detailed estimate of dispersion because an outlier can greatly exaggerate the range (as was true in this example where the single outlier value of 36 stands apart from the rest of the values. The Standard Deviation shows the relation that set of scores has to the mean of the sample. Again lets take the set of scores:to compute the standard deviation, we first find the distance between each value and the mean.
We know from above that the mean is 20. So, the differences from the mean are:15 - 20. Next, we square each discrepancy:-5. Here, the sum is 350. Next, we divide this sum by the number of scores minus 1. Here, the result is 350. This value is known as the variance. To get the standard deviation, we take the square root of the variance (remember that we squared the deviations earlier).What is StaleElementReferenceException? How do you resolve stale element exception in selenium?
This would be SQRT(50. Although this computation may seem convoluted, it's actually quite simple. To see this, consider the formula for the standard deviation:In the top part of the ratio, the numerator, we see that each score has the the mean subtracted from it, the difference is squared, and the squares are summed.
In the bottom part, we take the number of scores minus 1. The ratio is the variance and the square root is the standard deviation.