How To Handle Stale Element Exception - Selenium WebDriver Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello all welcome back in this one we're gonna look at stale element exception it is one of the trickiest exception and also one of the easiest exception tricky because most people don't know that real reason why it happens and easy because once you understand the reason the actual concept it will always be very easy for you to fix this kind of exception because unlike other exceptions there is no timing issue and there are no guesses to be made for wait times and synchronization issues so let's get to the concept let me just bring up Eclipse and the literal meaning of stale is not fresh and not new so in selenium-webdriver perspective it means that element is not new or element has become stale and that's when the exception happens so this line is really very important that for selenium-webdriver the element is not new or it's not fresh or it has become stale and that explains everything and I'm going to tell you what's gonna happen now so this is a simple code this is a test method where I'm just bringing up chromedriver and bringing up the website learn what let's coded the practice page right here and then what I'm doing is I'm finding a checkbox element BMW check and I am trying to click it so let me just run it and you will see what's happening and you will understand so if we bring this up it's gonna open Chrome and look at the right side there you go it click the BMW checkbox and that's what I was talking about right and it's pretty smooth the test was successful there are no issues and we're all good right so now what can happen and I'm just explaining the concept here so there is very important need to understand the basics so that you can apply it at any place right so in what case the element can become stale or not fresh imagine you found the element and then you performed some other action which has caused the page to reload or the page to change what happens is the Dom is destructed and the Dom is created again the element will show up again with the same attributes but it is created again in the Dom that's why the one which you previously found is not a fresh one now and you have to find it again so let's see what can happen here if I just do driver dot get and maybe driver dot get current URL this thing is gonna reload the page right this thing is gonna reload the page and if the page is reloaded the element checkbox element which I found on line number 20 this is gonna become still because now the page is reloaded and once the page is reloaded the element is gonna be visible to us it's gonna have the same attributes pretty much same everything but the one that we found is not fresh the page is reloaded Dom is built again it's created again we have to find the element again that's the thing so let me just run this guy and we will see the actual issue so it's gonna bring up Chrome and then it's gonna reload the page again and there you go the exception already happened that's there on the left-hand side let me maximize the j-unit console and you can see here stale element reference exception it says the element is not attached to the page document so it's pretty self-explanatory if you read this out it says that yes there was an element but it's not attached to the page document and why it's not attached to the page document because the page was reloaded and all the Dom was rebuilt right that's why it was not a - and it is showing still element exception now the solution is if you see anything that's changing the page if you see anything that's reloading the page and when I'm saying changing the page it doesn't have to be a full reload it could be possible that you performed an action and a part of the page is reloaded by Ajax calls or something and that built is the element so whenever something like that is happening may sure you find the element when you need it do not find them all initially and then use them later just find the element when you actually want to click it when you actually want to send keys to it do not find it ahead of time always find the element when you actually need it so that's very important and if I just move this statement down after the reload thing it should again work without any issues and that's what I meant when I'm saying find the element when it's actually needed do not find it before and perform other actions so it's free loading and there you go now it click the checkbox easily so that's the solution please understand the concept and you can apply it at any location wherever you see something is changing you're seeing steel element reference exception is happening you can simply apply this concept thanks a lot for watching this comment below if you have any questions please like and share this video with your friends and do not forget to subscribe us click the subscribe button to get more awesome videos like this and I'll see you in the next one
Info
Channel: Let's Kode It
Views: 19,148
Rating: undefined out of 5
Keywords: how to handle staleelementexception in selenium java, how to handle staleelementexception in selenium webdriver, how to handle stale element exception in selenium webdriver, how to handle stale element exception in selenium java, how to handle stale element exception, stale element exception in selenium webdriver, stale element exception in selenium java, staleelementexception in selenium webdriver, staleelementexception in selenium java, stale element exception
Id: yYQrtmNn6Uo
Channel Id: undefined
Length: 5min 32sec (332 seconds)
Published: Thu Aug 09 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.