Category: Selenium avoid stale element

Selenium avoid stale element

This Exception occurs when driver is trying to perform action on the element which is no longer exists or not valid.

selenium avoid stale element

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.

How to solve Stale Element Reference exception in Selenium Webdriver

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. Check Our Demo Website! Selenium Easy. Free selenium tutorials for beginners and experts. Selenium Tutorials:. 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.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:.

selenium avoid stale element

Recommended Reads. Subscribe Here!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. When you search for some keyword, text box's position get changed on page.

So In this case, Look and feel, Identifiers etc. This Is just example. You can have different situation or scenario. This will happen only some times but you must know how to resolve It. There may be some other reasons too for this exception. Now let us try to resolve or handle this exception.

In some cases, After refreshing the page or javascript action, Position of element will remains same but still you will get this exception. Unknown 1 November at Unknown 2 January at Arun 11 May at Unknown 21 August at Unknown 20 March at Newer Post Older Post Home.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.

What is StaleElementReferenceException? How do you resolve stale element exception in selenium?

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? Your email address will not be published.

Notify me of follow-up comments by email. Notify me of new posts by email. This site uses Akismet to reduce spam. Learn how your comment data is processed. Two reasons for Stale element reference. Example- If you are trying to click on link and getting the exception then try in below format. Sysout e. Comments 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.

For this I have to check as of now I dont have any scenario for this. Good article, really a wonderful way of explaining things. Hi Ashish, Wait does not work always. I love you men!!!

Selenium Easy

I was going crazy to solve this!!By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

I'm implementing a lot of Selenium tests using Java. 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.

Unbeknownst to me, BackboneJS was running on the page and replacing the element I was trying to click. My code looked like this. What would occasionally happen was the javascript would replace the checkoutLink element in between finding and clicking it, ie. Which rightfully led to a StaleElementReferenceException when trying to click the link.

I couldn't find any reliable way to tell WebDriver to wait until the javascript had finished running, so here's how I eventually solved it. This code will continually try to click the link, ignoring StaleElementReferenceExceptions until either the click succeeds or the timeout is reached. I like this solution because it saves you having to write any retry logic, and uses only the built-in constructs of WebDriver.

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. If this answer helped you, please go upvote jspcal's answer. Try doing the whole thing in Javascript:. I've found solution here. In my case element becomes inaccessible in case of leaving current window, tab or page and coming back again.

There could be a potential problem that leads to the StaleElementReferenceException that no one mentioned so far in regard to actions.Everything worked like clockwork. We had vouchers for everything all neatly organized in our arrival package. Can somebody give Helga G. We loved the notes that she put on the map for us about places we never would have discovered on our own.

We were so happy with our experience that we made sure to go to the Nordic Visitor office on our last day to say thank you in person to Helga. Iceland was spectacularly beautiful.

We have also always wanted to see the Northern Lights, and I would not hesitate at all to contact Nordic Visitor in the future to arrange that trip for us. Just got back from a 7 day tour of southern Iceland organized by NordicVisitor.

I was impressed that I went into this trip with no real knowledge of what I was getting myself, my wife and three teenage children into. The trip went on without a single hitch. The car rental, hotel reservations and activities were all perfect and we had a blast. Thank you and I would recommend this company to anyone. The only hitch was cost which was difficult to get a handle on.

In particular, the activities were costly. BUT, well worth every penny. We joined the Nordic Visitor tour party on the 25rd July following two days in Reykjavik on our own.

Nordic Visitor arranged for a taxi (with welcome pack and documents) to pick us up from the airport on Thursday and on to our pre tour accommodation, we then made our way to the tour hotel on the Saturday evening. Our guide Porsteinn (Steine) met us at the welcome meeting and told us our agenda. Sunday morning the tour set off at 9am the starting time for everyday. The tour went round the whole island (around 2000km covered) visiting all the major sites that can be reached from the N1 ring road.

We were of the 'comfort' level of accommodation and all the hotels and guesthouses were of at least 3 star quality with large clean rooms and en-suite facilities. Some had fabulous views as well. The guide constantly fed us information of the sites we visited in an informal but informative way with a construction fonts dafont Icelandic folk stories and jokes thrown in. The transport was a mini bus taking 16 of us comfortably from site to site with various comfort stops on the way.

There is a little walking to be done for example up to Dettifoss that cannot be avoided but all the walks were really strolls and nothing over taxing. The evening meals that were provided as part of the package were either in the accommodation or at local restaurants and were of a very high quality. There are optional extra's such as Whale Watching and the Thermal baths which although we pre booked could be booked on the tour. Transfer back to the airport was provided.

In summary this was a very well organised tour both before leaving home and whilst there. The program is varied and interesting, capturing all the major attractions. Would highly recommend the tour to anyone wanting to see the main sites but not do all the legwork of a self drive.

A friend and I (who live in Australia and UK respectively) were keen to do a self drive tour of Iceland as we could have autonomy and flexibility and avoid the tour bus hordes. We found Nordic Visitor through Google and I'm so glad we did.

selenium avoid stale element

We chose the 10 day Full Circle tour - which comes with a rental car, pre booked accommodation, detailed road map, local mobile phone, GPS and many suggestions for destinations and activities. We opted for a 4x4 vehicle (highly recommended as there is a fair amount of off-roading) and budget accommodation which was pretty much guesthouses. That's part of the deal anywhere really.

selenium avoid stale element

There is so much to say about the trip itself so feel free to send a message if you want more info. Nearly all the arrangements went without a hitch and what didn't go smoothly wasn't due to Nordic Visitor getting it wrong and indeed didn't ruin the trip at all (eg the guesthouse in Reykjavik wasn't aware of my travelling companion's very early arrival and were not prepared).

My only negative comment is some of the worthwhile sights might not be on their recommended itinerary so do a little research of your own.This question is in the form of a paragraph which How to Do True False Not Given Type of Questions in IELTS Reading.

January 2, 2017ufaber True False Not Given questions are considered to be the trickiest questions in IELTS Reading where candidates get confused a lot. Let How to Do Yes No Not Given Questions in IELTS Reading. January 2, 2017ufaber Yes no not given questions are similar to true false not given questions.

How To Handle staleelementreferenceexception Selenium Webdriver

The difference between the How to Do Matching Heading Type of Questions in IELTS Reading. December 31, 2016ufaber One of the most difficult questions that students face in IELTS reading is matching heading type of questions.

Candidates appearing for the How Well is Your Trainer Training You for IELTS Reading Academic. December 19, 2016ufaber There are many IELTS training institutes with a number of IELTS trainers. The advantages provided by English as a global language IELTS Writing task evaluation and model answer August 21, 2017K. One thing to notice hereListening Tips How to Do Completion Type of Questions in IELTS Listening.

January 3, 2017ufaber In the IELTS listening exam, you can find completion type of questions in the last section i. The question How to Do Diagram Type of Questions in IELTS Listening. December 28, 2016ufaber In IELTS Listening test, one of the easy questions is of diagram type that is quite common in Section 2.

In such questions, you are given a diagram which can of any equipment, instrument, gadget, manufacturing process or anything which How to Do Flow Chart Type of Questions in IELTS Listening. December 28, 2016ufaber You may come across flow chart type of questions in IELTS listening exam, probably in Section 2.

Flow chart is How to Do Matching Heading Type of Questions in IELTS Listening. December 28, 2016ufaber One of the tricky questions apart from multiple choice questions in IELTS listening are the matching heading type of questions. As it can be determined by the term itself, matching heading involves matching a list of questions to another list How to Crack Section 1 of IELTS listening November 25, 2016ufaber IELTS listening section 1 is perhaps the easiest part of IELTS listening exam.

If you can get all the ten questions right in this section, it would How to Do MCQ Type of Questions in ILETS Listening. October 27, 2016ufaber Did you notice a spelling error in this article title. The reason why this spelling error is highlighted in the title is to draw your attention towards the word 5 Penalty Areas for Indian Students in IELTS Listening October 26, 2016ufaber Since Indian students are writing and speaking in English with Indian accent, with not much exposure to foreign accent, it can be the cause of some penalties they may face in IELTS listening module.

Let us know what these likely How to Crack Section 4 of IELTS Listening October 25, 2016ufaber In the previous article, we discussed how to crack IELTS Listening Section 3. In this section, almost all the times, you find sentence How to Crack Section 3 of IELTS Listening.

thoughts on “Selenium avoid stale element

Leave a Reply

Your email address will not be published. Required fields are marked *