How To Handle No Such Element Exception - Selenium WebDriver Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello I'll welcome back in this one we're gonna learn about no such element exception it is one of the very popular interview questions and this is one of the very important concept as well because we all make some small mistakes and we all see this issue so let's bring up eclipse and let's see the reasons and how do we resolve these things here I have the code which actually just sets up the chrome driver and initializes it and just getting the base URL so that the website is up and here is the name of the website that we're gonna be using for this concept there are basically three main reasons why we see this issue first one is the timing issue timing issue happens when selenium webdriver tries to find an element but it has still not rendered on the application and there is this time lag which causes this exception to happen so let's see what is this in reality so once we open this application what I'm gonna do here is I'm gonna find this login link and click on this by the time it's gonna take some time to reach this page and we will also try to find this email address field and type something on it and let's see what's gonna happen so we are already going to the website by using driver or get now what I'm gonna do here is driver dot find element by dot XPath and I'm gonna put the XPath of the login link so let's go to the main page bring up chrome dev tools and inspect the login link here at the bottom I can see that it has an href with sign-in right so what I can do here is I can build the XPath with a at the rate href equals to sign in right and then I can click this link the next thing I want to do here is I want to find the email field right so let's do this email field equals to driver dot fine element by and I know this guy has an ID so that's what I'm already typing it but we will take a look at it again so let's click on login and go to this page again inspect this field and there you go it has an ID called user underscore email let's put it here and once the element is found we're gonna say email field dot send keys and I can just type in something like testing if you'll notice I have commented this line out and we'll talk about all of these things let me just quickly write like this and run the code so that we can see this in practical you can use J unit test ng anything that you like the important part is to understand the concept so it's gonna bring up Chrome browser click on log in and boom failed even though we are on the page and as a human we didn't even see any time right we thought that it's pretty quick and we are not seeing anything getting typed but if you look at the back it has already failed with no such element exception if I go at the bottom scroll down and this is the line from interview questions dot no such element demo and this is the line from the program so this line failed and I'm gonna show you what that is so if I go to the project and the source I have a package called interview questions and no such element demo is the class that I created I would suggest never create a class with the same name as the exception no such element exception do not use that that's why I'm using no such element demo so from this class this line failed and the reason is because selenium webdriver after clicking the login link thought that I need to actually just find the element for email field and start typing on it but when selenium webdriver started doing this the page was not loaded it was still moving it was still rotating but it was not completely loaded and this element was not rendered that's why it failed to find this now how do we resolve these things that's important right so this is a timing issue what we can do here is begin to a few tricks there one of them is we can use implicit bait so if I uncomment this line which is just using implicit wait for 3 seconds this code is gonna run fine because now what happens is selenium-webdriver is gonna use this implicit wait to find the element so let's run this code tell it and let's bring up the chrome so login and now we can see testing it pretty much quickly did that right so all good the test is successful and we have no issues sometimes even though when we are using implicit wait we can still see this exception because some of the elements are really tough and they actually give us a hard time sometimes they may take more time than the implicit wait and they're like really slow so for those ones what we can do here is we can use the explicit bait for this I'm going to command the implicit bait because now we are trying to find with explicit wait so we want to comment this out to make sure that this guy has no effect right so here I'm gonna type webdriver wait wait equals to new webdriver wait and this guy takes driver and the time out so this is the setting and there would be few import statements so make an order of these you can pause the video and note them down and once this is there what we're gonna do here is we're gonna pull this guy or you know what let me just comment this thing out and put it here now we're going to use bait dot until under that we're gonna say expected condition not visibility of element located so visibility of located and this guy is gonna take thee by dot let me just put these things in the next line so that we have a little more space to see what's happening now by dot ID and the idea is we already know what the ID is so explicit weight is gonna do the trick here and need to put the equals there now we can pull this line at the bottom after finding the element you can pay attention here that we are not using the implicit weight we are using the explicit weight here and now we are sending the keys so I'm gonna just quickly right-click and run this code as tail it and let's see what's gonna happen it's gonna bring up the page login and there you go boom it successfully typed on the field and we are not seeing the exception so these are two ways to wait for an element and resolve these kind of exceptions the second one is it's a really silly mistake we often do sometimes what we do is we do not use the correct locator or correct locator type let's say I am on the home page right I was on the home page here and then I clicked on the login link now I clicked on the login link and I have typed something then for some reason maybe my test is again click on this link and if I try to click on this link thinking that it's the same link this is one of the silly mistake because it's just that we are not paying attention there's nothing to do with the selenium webdriver or the web application so let's see if the web application says that you have to again click the login link so what's going to happen I'm just gonna type the same thing so driver dot fine element by dot XPath and what is the login link this guy right and dot click so this is just the continuation and let's see what's going to happen I expect the code should fail because I know what's there in the web site and which talk to you just in a second and boom it already failed because I can see the backside from here and what happened is if you see it the browser it type testing but it failed after that so if I scroll down and click on this one it points to line 33 which is finding the login link and we must wonder that it found the login link here why is it failing this time so now let's go and inspect the element so I'm just gonna inspect this element again and now there you go you see the href attribute is completely different it's not equal to what we are using it's not equal to slash sign underscore in we can see that it's the complete URL right so this is just a mistake that we didn't pay attention to what is the locator thinking that it looks same but guys this is one of the very frequent mistakes sometimes we are not on the correct pace sometimes we're not using the correct locator sometimes we may just miss things like instead of using bide or XPath I may tie by dot ID and still use XPath and this is gonna feel always make sure that you use the correct locator correct locator type and then it should work so for this to work we can simply find the correct locator from here and fix this guy then maybe what I can say here is contains href and done so because we are just looking for contains now it should just fix this let's run it and see what's gonna happen and there you go a type testing and then clicked on login again and the test is successful now we're going to take a look at the final one which is element is in an iframe and this is like really tricky so let me just show another element for this what I'm going to do is I'm going to go the practice page and on the practice space this is the search bar I'm just gonna right click inspect the element and this guy has an ID called search courses try to find this element and type something in it right so let's do this driver dot get and we need to actually move to the practice page for this so let's first try to find the element by dot ID and then dots and keys and maybe I'll search for Java and that should be now let's get the proper link for practice page which is this and put it in the code so now we are expecting that they should work completely fine right and if it has any issues then we will get to know so let's just quickly run the code or maybe you know what we can just comment out the other part here because we really do not want it and we can uncomment the implicit bade and this is already discussed so that's why I'm commenting it out now let's just right click and run this code as j-unit it's come bring up chrome and it's just gonna fail yes it did fail no such element exception and if we click this one it says it failed on line 38 which is finding the search courses field and now we wonder man I mean I just figured out that this is indeed the correct field and why is it not working right so what could be the reason now let's just go ahead and inspect the element again to understand the details when we go to the element if we click on console we can see this drop down and if this drop-down has iframe here then it means that this element is under a separate iframe and when I'm hovering my mouse on it you can see that the particular portion of the website is highlighted because that's the iframe section so this is how we find that if the element is an iframe or not and if I scroll up on the website I should see iframe somewhere and there you go if I mouse over this iframe the particular part of the page is highlighted so this shows us that the element is under an iframe now this iframe has an ID course iframe it has a name also we can use either of them I'm gonna use this to switch to the iframe so this is what we have to do driver dot switch to dot frame and put the ID or name of the frame this guy is gonna switch to the iframe then we can work with any element under the iframe but remember once you are done with the iframe always make sure that you switch back to the top window the parent or the default content so driver dot switch to dot default content and then once you switch to the default content you will be back to working with other elements so for instance if I inspect this element and Google console I see top here I do not see any iframe right so this means that this is the completely the main window if I run this code now I should see the expected result which should be a successful test and yep there you go it found the element anytime Java in there so that's done right now this thing no such element exception this thing can sometimes be asked in interviews in different ways they may ask you what are different types of exceptions and you have to name this exception also within your answers and they may also ask you when do you see no such element exception and how to handle it now you know three ways why we can see no such element exception and how we can resolve all of them thanks a lot for watching this command 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 in the next one
Info
Channel: Let's Kode It
Views: 25,622
Rating: undefined out of 5
Keywords: selenium webdriver interview questions, no such element found exception in selenium webdriver, how to handle no such element exception in selenium webdriver, nosuchelementexception unable to locate element, how to handle nosuchelementexception in selenium webdriver, no such element exception in selenium webdriver, page object model framework, selenium webdriver tutorial, nosuchelementexception in java, no such element exception in selenium
Id: y6SYtAh-kCk
Channel Id: undefined
Length: 15min 39sec (939 seconds)
Published: Mon Jun 25 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.