Handle Dynamic Radio Button and Check Box in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome back to learn - Automation calm this is Mukesh in this video we'll see how to handle a dynamic radio button checkbox in selenium webdriver so before jumping into the actual program let's see what exactly we are going to cover so we'll see how to handle the web element which has the same property so we will be using here find elements method of selenium and then we'll see how to click on a specific element based on the specific values so before jumping into the code let me show you here we have four let us take these two checkboxes if you see these two checkboxes which is singing and coding here ID is singing and code but you can see that name is same here so for both the checkboxes the name properties language and language and if you see the same thing for this currently we have four checkboxes in this normal scenario sorry this radio button so Java sheesha Python Ruby all these for having a same property for name ID and type so IDs unique a Java sheesha Python Ruby but if you see the name is language language language and language right so if I try to write my X park like search for this specific input box we are named equal to length not input box exactly I'm looking for particular check box or radio button so when I say find an input tag where name equal to Lang it is giving me this check boxes and radio buttons right but I need only and only radio button so I can add one and condition here and I can say a trick type equal to radio so you can see now it is only returning the for web elements so another question comes when we have the same property and we are matching with multiple web elements how to deal with that but we can see here we have some text s will write for every here you cannot see the value but if you have the value you can select using a value as well so let us take this scenario first we will take one so let us see how to handle this kind of scenario so I will right click and I will create a new class and class I will say handle radio buttons I will take up the static void main I mean please the font so let us start with a browser first I will start browser so I will start roll and I will specify the which we will take is this I will add this URL in the description now we need to work with radio button so we have written our connect path we are looking for specific web element so since you are working with multiple web elements we have to use fine elements by XPath so what exactly this XPath will return this XPath will return for matching node since using find elements method here it will return my list of web elements and I will say let us say radio we need to import lists from Java dot util we need to import web element as well from selenium now this is a list now I will write one enhance for loop and enhance for loop we need to take web element every time the cost we will get list of web elements so every time this radio will return me one unique web element or else you can take direct for loop which will start with 0 which will go till the number of values we have so we'll say size here and I plus plus so every time it will return me open web element I will start with 0 and I will put till the number of elements I have since you are using list to get the value from the list we use get method so I will say this radio not kept if I pass a specific index it will return me that particular element since you are running of all every time it will give me a new web element that I will store in a separate variable let's say local radio is ready but I'm having ID and value so let's say I want to print the values of all the checkboxes on so I will take this local radio dot kept attribute value so when I say value it will return me as a string so we'll say value and every keep ring and I will save values from radio button well you only three lines of code we have written we just start River you are we found give me all the radio buttons which have the name attribute s Lang it will return you fold we'll start with a for loop that will start a zero it will go to the number of web elements we have every time we'll pick one web element and we will get one attribute called value so value is nothing but this Joshi shall fight in 2d and it'll print in the next program we'll see how we can select based on the value as of now it will simply print so it it started very fast and helped it has printed all the values I added travel dot foot so it's closing very quickly quickly so let's run it again and you can see it is printing the values but we don't have to print we need to pick on the link right or radio button check box so we'll add a spore condition here and we'll act if the value which we have fetching if it is equal to or equal you know case Ruby then click on it so this is our web element so I will say local radio dot click so this is how it works you need to only add one condition if value equal to the width and click on it let me show you this in debug mode I will put one debug point at line number 32 and I will write it he began java application this is previous when i we closed okay so you can see our test stopped here so what that's stopped here we have one valuable Java you can see here and we are comparing with Ruby so first it will print this print it that will compare if Jo equal to goofy no it will just go to another loop and you will see right now I value is zero here so when I say one it will increment by one will take the second web element it will take this value and this time value we have is C sure so it will print she shall hit it will again check whether good it will be no so again keep iterating the move this time it is checking the value which we have is Python again not satisfied now suspend it will match because it will take the fourth element which is cooking not will compare now you can see here right now no other options you can see it click to be has quit and now it is again checking the list which is empty now it will come out of this form of that so that's it you can see it's very straightforward you need to only understand how you're traversing the list if you understand how to traverse the list same thing will be able to people for checkboxes as well so let's say if I have two checkboxes so how can I do that I will search for let me show you if I right-click on any of this and say inspect I have ideas singing ideas coding but the name is Lange and type equal to checkbox I can write a small XPath where I will say find one input tag where type equal to checkbox right so you can see it's messing with two you can see this XPath will if I read this XPath this XPath will match with two labelled event because in this page we have two checkboxes so if we see here it's showing two up to same thing if you right here it will show you two matching node one is this one is this so how to handle this kind of scenario I will again go back in the same program and here I can write I will write driver dot find elements make sure you have to take find elements by XPath and xpath I will use this checkboxes so definitely this will return the list of web elements I will store English spoke web elements and should be small and I will say check box now I can run a four loop which will start with zero then it will go till the number of check boxes we have so I will say check box dot size then I plus plus now every time I will take one check box from this and I will say get off I it will return me specific web element so I will take ele dot ket attribute ID so it will return the ID of that particular element and here I will check if I D not equal if node is equal to code then ele dot click and break the loop s whole because if you do not give break it will keep checking all the web elements which is not required so in the first section we have handled the radio buttons in the second section we have handling the check check boxes we have found all the check boxes then we are running a for loop we will take one by one we'll take ID every time if I'd equal to code then it will click and then it will break so let me run this normal mode you can see it's clicking on Ruby and it's clicking on coding itself so this was the possible scenarios so like this you can name not exploring the different web application where you will find the same properties and then you can use this find elements and you can iterate using lists you will find a pattern there must be some value associated with the every verb element so I use this basic for loop you can use enhance for new best friend which we have done in the previous videos so that's all I have for this video if we still have any doubt feel free to reach out to me okay short one a direct laugh I have not emotional thank you so much for watching this video have a nice day bye bye
Info
Channel: Mukesh otwani
Views: 120,384
Rating: undefined out of 5
Keywords: Selenium Webdriver, Test Automation, Radio Button, Checkbox
Id: EQu5rQCPsZU
Channel Id: undefined
Length: 16min 47sec (1007 seconds)
Published: Sun Aug 28 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.