How to handle hidden webelements in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone welcome back to learn - automation comm today we are going to discuss how to work with disabled or not disabled I will say I will say hidden web elements this is one of the important and very interesting topic in selenium where you will deal with some invisible web elements so let me get started let me show you the high-level agenda so we'll discuss what is exactly this hidden web elements and why in some application you will find this hidden web element and you will notice once you start working on the hidden web elements selenium will throw you an element not visible exception so how to overcome with that will see some solution as well so let me go to eclipse and I have one sample application ready here so I will give you the sample link in the description of this video so you can also check so I created right now only four five posts but soon I will be creating more so let me start inspecting one by one so you can see I have what - radio button here one is male one is female so if I click on female you can see I have an input which is highlighting the female radio button ideas female and type equal to radio and if you just click on this male you will see I have two input box right with the same one is I equal to male type equal to radio but if you see when more I have with hidden attribute so you can see ideally I should have two radio button on the screen because I am using two but one is hidden so you are not able to see one more radio button so now if you try to write one XPath let us say I can see the input box male so this XPath is generating 1 X bar plugin is entering this XPath and you can see here it is two matching node right so what if I try to on this idly it should click on mail but here it is matching with two notes so just quickly will create one small class and I will say Firefox hidden web elements I am just creating a simple Java program so if we are using J interest in G then you can use that as well quickly I will start my browser using webdriver Drive or equal to new Firefox driver then I will pass the URL which application I have created for this demo so I will just use that application now let's maximize that as well so driver dot manis dot windows dot maximized and let me do this in opposite way so now if you see if I try to click here driver dot find element by X path and the X path which we got from here let us take this ID directly you can see we have the same ID for both the radio button right so let me take the ID and let me say click so when you say I'd equal to male then selenium will get two web elements and this is the pro you can say the feature of selenium when you have multiple node matching with the same ID selenium will click on the first one I will show you with the help of an example when you you write as specific XPath or ID and through that particular locator you are getting multiple web element then selenium will try to click on a first one and if you can see here the first one is nothing but our hidden web element right then if you try to click on a hidden web element let us see what will happen if I right click run as java application and this is one of the most important questions in interviews as well they will ask you in a different way like what are the challenges you have faced in your web application then this is very normal scenario in general web application you will find some hidden web elements and you need to work with you need to find out so once this page will load it is not clicking so you will see here webdriver is giving very meaningful exception like element is not currently visible so webdriver is not able to interact with so whenever you are giving getting this lengthy exception then don't panic just try to read it so if you go in a reverse direction then you will find here it is actually coming in this particular line right we created this class just now like Firefox hidden web elements and the exception is coming at line number 18 so if I click here it is never getting here now when I realize it's matching with 2 and when it's matching with 2 its clicking on the first one which is hidden so let us do one thing let us see if I try to get the X and y coordinate so if I say get location dot get X and get Y so what is XY coordinate so in selenium or not in selenium if you see in Qt PS will if that element is currently visible on the screen then definitely it should have some X&Y coordinate right then only it means it is displayed so first I will check whether this particular radio button or any other web element is having some positive x and y Fe s then only I will move forward so let me show you I will say int X and in the same way I will say int Y then if it contains some x and y coordinate then we will see some other approach if X why both are zero it means that element is not visible so I will say x coordinate plus X in a same way I will do for Y so this is the problem statement solution I will show in the next program how we can solve this problem so as of now what this gate location will do so it will check the location of that particular element and get X will simply return you the x coordinate in the same way get Y will return the y coordinate so if you see the description of this method it says a point containing the location of the top left-hand corner of the element so let's run it and run as java application and you will notice since we already know it's a hidden web element you will get x and y coordinate as 0 let me close the previous instances otherwise it will confuse you so I will be proposing only one solution but you have multiple solutions to overcome with this it's loading I have uploaded three more solutions on my blog this is the fourth one which I am proposing in this video so you can use any of this so if you see x and y coordinate is 0 0 because that element is not currently visible so let's do this as we know it's matching with 2 so now we are going to use one additional method find elements so let me close this so it will be full screen so what I will do I will use driver dot find elements because right now we are dealing with multiple web elements so till now I have used only this one find element which simply returns me the web element but today we will be using this find elements so what exactly does it will find all the web element for that particular locator and it will return you the list of web elements the first method will only return you the web element because you have given only one but this find web elements will return the list of elements so let me use find elements this time and I will give ID so in this case definitely it is going to return me to web elements so I will be storing since it will return me list of web elements you can see in the left side left list of elements so I will be writing list of web elements and here I will give some meaningful names a radio so it will give you two errors to put mouse over on the list and you can see we get three options don't get confused just always click on the Java dot util and you need to import this verb element is Vil which is coming from the selenium so now you have this radio object which is nothing but a type of list of web element so if you want to know how many radio buttons are messing with this particular ID I can write like total web elements or total radio button then I can use this object radio then I can call one predefined method call sighs let me type directly total radio buttons radio dot sighs now the logic which we are going to use what we will do will run a for loop will get every element one by one then we will check if the X&Y coordinate is equal to zero then go to the next wave element so what I will do I will start my for loop with I equal to zero then I will go till the number of elements we have so definitely have a for loop will start with zero and radio size is two right so it does for loop will run two times zero and one and if you do not want to get confused then I can write the code in one more line I will sit count this count I will mention here and same I will mention here so it will start with zero till the number of web elements we have which is nothing but one and we'll take web element one by one if x and y coordinate or even you can check with x coordinate if it equal to zero then go to the next web element so what I will do when when we run this first time we will say radio dot get get is a method of this list so in this you need to pass the index so I am passing the index is e I value and I will say the gate location get X so here I will get the x coordinate and I will write if X is not equal to zero then there's some problem with this okay if X is not equal to zero then get that element using this then click on it and break the for loop if xn does not equal to zero it means that particular web element it will definitely exist and it will click and finally it will break the loop if you do not break the loop then what will happen it will keep checking all the web elements so the program flow will be it will start with zero it will go two times so in the first iteration I will be zero so what it will it will take radio list it will get the first web element it will check the location X then we'll check if X does not equal to zero then click at the right break so let me run this two times one time I will show you with normal mode one time with debug mode so it will be easy for you to understand then I will close this so what you can do you can create a library for this for your framework and whenever you find such type of exception which is element not visible exception then you can use this XY coordinate concept as well so will notice yes it is clicking on male and it says total radio button r2 as simple as that so let me complete this code once again with let me do some small modification because it seems quite complicated if you are completely new so what I will do I will break this into two part like I will get the elements in a separate variable ele it means when you run for loop it will return your first web element from that web element I will take the location get x-coordinate if yes then that element only I will seek so let me show you through a debugger statement I will put one debug point at line number 32 and let me close a key in so my control will stop here I will put one breakpoint at line number 24 as well this time let us put in a debug mode so when we are getting this kind of questions like what are the challenges you have failed in real time then you can tell one example of hidden web elements second is if you have same ID or X path which is mating with matching with multiple nodes then how you will handle so using this find elements you can handle so now it came to debug mode so you can see the control is stopped here at line number 24 so if I press f6 I got one variable called count equal to two and now I will press f6 so you can see this system this lamp man over 25 executed so this is the statement now this is really very interesting part now if I try to run this let me open this variable window so I values 0 yes because we started with 0 so as of now I value is 0 so it will take the first web element and it will store in this ele object right so now you we got ele now we will see get X so X is obviously 0 for the first web element now it is checking if X does not equal to 0 which is false so again it went to the for loop now this time you will notice it will increment by 1 and you can see I equal to 1 so definitely it will pick the second web element yes it is picking now if we try to take the x-coordinate naught is 240 because that second element is visible now definite it will go to if a statement and if you see right now it is not clicked but when I execute this statement it clicked so you can see it got click now if I say break it will come out of this for loop and it will exit your program so this is how you can debug your program so just try this a from your side it works fine for every hidden web elements I have tried almost with different cases now one more example before ending up this video so let us say I designed all these four radio buttons in a such a way when you try to see every radio button having different ID like Java C sharp Python Ruby but you will find the name language equal to like same language right name equal to length so what if I take this name and if I try to let me create a separate copy of it paste and I will say it's one so this time let me take let me delete this first so question comes what if I use driver defined element only and I will use this name name is lang right yeah lang here so if I use this and if I say dot click so now question comes if I use this way then what will happen as of now we can see lang is matching with four nodes and I am clicking directly so in this case selenium will by default will take the first web element with this particular name and it will click so in our case we have 4 radio buttons with the same name called Lang so it will click on Java so this is again one tricky question which you might face in your real time and in your interviews film then I will show you one more dynamic way but that in the next video because this video will only focus on how to work with hidden web element and how to solve element not feasible exception so you will see it is clicking on the first option because it is matching with four so it will take the first wave element and it will click now the another scenario which I am going to talk in the next video I want to write a general method like if I pass Java then it should click on this Java check box if I pass a Ruby then it should click on the Ruby check box so we want to make a little bit dynamic so based on my parameter or based on my input it should take the respective ready button that we'll see in the next video how we can make it dynamic so thank you so much for watching watching this video if you have any doubt you can comment below and feel free to contact you can comment below and don't forget to subscribe my channel thank you so much we'll see you in the next video bye bye
Info
Channel: Mukesh otwani
Views: 113,866
Rating: undefined out of 5
Keywords: How to handle hidden webelements in Selenium Webdriver, Selenium Webdriver, Automation, Selenium Webdriver Tutorials, TestNg Tutorials
Id: 2eEr0jtmpwE
Channel Id: undefined
Length: 21min 19sec (1279 seconds)
Published: Wed Aug 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.