Session 29 – Selenium with Java | Handling Check Boxes,… — Transcript

Learn how to handle checkboxes, radio buttons, and alerts in Selenium with Java, including locating elements and performing click actions.

Key Takeaways

  • Checkboxes allow multiple selections, unlike radio buttons which allow only one.
  • The same click() method is used to select and unselect checkboxes in Selenium.
  • Proper locators such as XPath are crucial for interacting with specific or multiple checkboxes.
  • Selector Hub is a useful tool for capturing element locators efficiently.
  • Looping or capturing multiple elements is necessary for handling multiple checkboxes programmatically.

Summary

  • Introduction to handling basic web elements in Selenium such as checkboxes, radio buttons, alert windows, and popup windows.
  • Explanation of differences between checkboxes and radio buttons, emphasizing multiple selections in checkboxes.
  • Demonstration of locating checkboxes using XPath and other locators with the help of Selector Hub.
  • Step-by-step guide on selecting single, multiple, or all checkboxes using Selenium WebDriver in Java.
  • How to write proper locators for specific checkboxes, e.g., selecting 'Sunday' checkbox.
  • Using the click() method for both selecting and unselecting checkboxes.
  • Capturing multiple checkbox elements using common attributes and XPath for batch operations.
  • Creating new packages and classes in Eclipse to implement checkbox handling code.
  • Use of Selector Hub tool to inspect elements and copy relative or absolute XPath.
  • Discussion on best practices for handling multiple checkboxes without writing repetitive code.

Full Transcript — Download SRT & Markdown

00:05
Speaker A
So from today, we're going to start learning how to interact with different types of elements. First, we'll start with the basic elements, like how to handle alert windows and how to handle popup windows.
00:19
Speaker A
and then how to handle drop downs and so on so first let us start with uh check boxes how to handle checkboxes and radio button check box is almost the same but only the differences in checkboxes we can select multiple
00:35
Speaker A
And then how to handle drop-downs and so on. So first, let us start with checkboxes, how to handle checkboxes and radio buttons. Checkbox is almost the same, but the only difference is in checkboxes we can select multiple options.
00:50
Speaker A
the checkboxes not only selecting the checkbox so for that I'm taking one example here okay so let's go to uh this application here I have different uh checkboxes Days Like Sunday Monday Tuesday and so on there are multiple
01:14
Speaker A
But in the radio buttons, we can select only one option at a time. And the methods and way of handling both are exactly the same. So now, let us see how to handle checkboxes. There are various types of operations we can do on the checkboxes, not only selecting the checkbox.
01:30
Speaker A
check boxes or first three checkboxes or I want to select the last two checkboxes or last three checkboxes randomly see how it is possible so let us see one by one so normally if you want to handle the checkbox we will identify this
01:47
Speaker A
So for that, I'm taking one example here. Okay, so let's go to this application here. I have different checkboxes, days like Sunday, Monday, Tuesday, and so on. There are multiple checkboxes.
02:05
Speaker A
it is already selected when I use click method it will unselect for both actions we use same method now go to eclipse and create new package for today n new class and here handle check boxes okay so now I'm going to
02:46
Speaker A
So here, we can do different types of operations. For example, I can select only one week at a time, or I want to select all the weeks, or sometimes I want to select only a few checkboxes like this, first two checkboxes or first three checkboxes, or I want to select the last two checkboxes or last three checkboxes randomly. See how it is possible.
03:05
Speaker A
operation suppose I want to select specific checkbox specific only one single checkbox so we need to write a proper locator for it so let us say I want to select something called Sunday let's inspect that element and you can go here selector Hub
03:29
Speaker A
So let us see one by one. Normally, if you want to handle the checkbox, we will identify this element or we locate this element, then we use click action, click method. For selecting and unselecting, for both operations, we use the same method, click.
03:44
Speaker A
also having same class if I look at here so we cannot use it ID is different from one element to another element so we can use ID and uh one more thing in selector Hub if you want to capture xath so normally
04:00
Speaker A
Okay, and if it is not selected, the click method will select the checkbox. And if it is already selected, when I use click method, it will unselect. For both actions, we use the same method. Now go to Eclipse and create a new package for today and a new class, and here handle checkboxes.
04:12
Speaker A
other way is you can just open the selector Hub it will open the window like this and here you can take this selector H inspector and then uh specify or show any element whichever you want and then it will show you expart so these are the
04:30
Speaker A
Okay, so now I'm going to create WebDriver driver equals ChromeDriver. Control shift O, so when I say control shift O, it will automatically import required packages. So this is an application, and then we maximize the page.
04:42
Speaker A
click and go to selector Hub and here you can find these options directly you can find here so copy absor xath copy relative xath okay you can get directly capture from here so I'm just copying relative Expo and go back and and find
05:00
Speaker A
Now the first operation, suppose I want to select a specific checkbox, specifically only one single checkbox. So we need to write a proper locator for it. So let us say I want to select something called Sunday. Let's inspect that element, and you can go here selector hub.
05:26
Speaker A
option when I run as Java application okay now we can see here the checkbox is got selected suppose uh I want to select all the checkboxes I want to select all the checkboxes then what we can do so this
05:55
Speaker A
And you can see the XPath, and if I look at attributes, class is there, ID is there, type mostly. Class attribute may be the same for other elements if you look at here, and for other class, other checkbox also having the same class if I look at here.
06:11
Speaker A
this element like this every element and then you have to perform the click action suppose you have a five elements or 10 elements you need to write 10 statements like this every element you have to click and suppose you have a 10
06:24
Speaker A
So we cannot use it. ID is different from one element to another element, so we can use ID. And one more thing, in selector hub, if you want to capture XPath, normally what you're doing is you can just right-click on element and go to inspect, and it will show you the selector hub here, and here you can see all the locators.
06:40
Speaker A
one second one select all the check boxes so to select all the checkboxes first of all we need to capture all the checkboxes okay into a variable we need to capture all the checkboxes once we capture all the checkboxes then we have
06:59
Speaker A
So this is one way we can see, right? And the other way is you can just open the selector hub. It will open the window like this, and here you can take this selector hub inspector and then specify or show any element whichever you want, and then it will show you XPath. So these are the two ways we have.
07:12
Speaker A
checkboxes means what I want to capture multiple web elements so to capture multiple web elements what we should do now we have to write One X paath or any other locator which will able to match only these checkboxes because in this
07:29
Speaker A
Other than this, there is another way. So without opening selector hub, we can still get the XPath from the selector hub. And for that, what you can do is you can simply say right-click and go to selector hub, and here you can find these options directly. You can find here, so copy absolute XPath, copy relative XPath.
07:48
Speaker A
X Parts directly especially when you're locating multiple web elements most of the times we have to write our own locator now go to selector Hub and then just observe if you want to capture all these checkboxes we need to find one
08:04
Speaker A
Okay, you can get directly capture from here. So I'm just copying relative XPath and go back and find that element. So driver.findElement by XPath dot click. So this will select a specific checkbox if you want to select only one checkbox or two checkboxes. You can just locate that element first and then select it. This is simple, direct option.
08:20
Speaker A
d tag inser this input tag this is actual checkbox there's another D tag inside this there is another input tag and you can see every div is representing one section and inside the D we can see input tag these input tags
08:34
Speaker A
When I run as Java application, okay, now we can see here the checkbox is got selected. Suppose I want to select all the checkboxes. I want to select all the checkboxes, then what we can do?
08:50
Speaker A
find one common attribute and if you look at here what is the common attribute which we have here Class Type we cannot use okay okay type is a checkbox okay that is also fine but before that if you look at
09:05
Speaker A
So this is an important scenario because in checkboxes we can select multiple checkboxes. So how can we do it? So if you want to select multiple checkboxes, normally if I don't want to use any looping statement, you have to identify this element like this every element, and then you have to perform the click action.
09:20
Speaker A
to use this class attribute to locate uh the element along with the xath Okay so for example if I say okay so let us see only class name okay so let me capture the class name I'm writing only class so this is a
09:46
Speaker A
Suppose you have five elements or 10 elements, you need to write 10 statements like this. Every element you have to click, and suppose you have 10 elements or 20 elements, then it is very difficult to write so many statements.
10:01
Speaker A
here I'm using class equal to form check input okay form check input now can just observe nine elements are matching but how many elements we required only seven elements but it is matching nine elements you can see this x paath is
10:17
Speaker A
So simply what you can do is we can write a looping statement. Through loop, we can select all the checkboxes. So first thing is step one, second one, select all the checkboxes.
10:36
Speaker A
this okay if you writing xath using a class it is not exactly matching with seven elements it is also matching with some other elements so now we need to filter these nine elements we have to filter so how to filter this so you can
10:54
Speaker A
So to select all the checkboxes, first of all, we need to capture all the checkboxes into a variable. We need to capture all the checkboxes. Once we capture all the checkboxes, then we have to write a looping statement. Through the loop, we can repeat each and every checkbox one by one and then select.
11:11
Speaker A
button text area so that doesn't have any uh type attribute right so we can filter by additionally adding type attribute so how can we add additional attribute here additional attribute using end end at theate type equal to checkbox okay now seven elements are
11:41
Speaker A
So to select all the checkboxes, first we need to capture all the checkboxes. So how to capture all the checkboxes means what? I want to capture multiple web elements.
11:56
Speaker A
if I use only class it is matching with what nine element Els so I want to filter okay for example if I use only type attribute we will see whether it is locating element or not only type attribute see here 12 elements are
12:11
Speaker A
So to capture multiple web elements, what we should do now? We have to write one XPath or any other locator which will be able to match only these checkboxes because in this page there are so many elements, but I want especially get only these seven elements.
12:27
Speaker A
this is a class attribute this is a type when you combine these attributes then we are able to locate exactly Seven Elements so this is how we can write our own customized xath according to your requirement now let me try to use
12:44
Speaker A
Okay, like that we need to write a locator. Now let us try to write our own locator because selector hub cannot give this type of XPath directly, especially when you're locating multiple web elements. Most of the times, we have to write our own locator.
13:00
Speaker A
currently we have so many attributes right so unnecessarily why we need to go with nber elements not needed so we can directly use existing attributes and we can create our own exper and try to use it okay now so to select all the check
13:18
Speaker A
Now go to selector hub and then just observe. If you want to capture all these checkboxes, we need to find one common attribute, right? So if I look at ID, ID is different attribute, so by using the ID we cannot get all the checkboxes because ID is different from one element to another element.
13:33
Speaker A
xath and this is the xath of the elements so I'm not doing any action directly I'm just keeping semicolon so this will return multiple web elements means multiple checkboxes so I'm going to store in a variable called checkboxes
13:51
Speaker A
I look at here how these elements are arranged. So this is a div tag inside this input tag. This is actual checkbox. There's another div tag inside this. There is another input tag, and you can see every div is representing one section, and inside the div we can see input tag. These input tags are actual elements of checkboxes.
14:14
Speaker A
letter okay so now what we have done here is we have captured all the checkboxes into this so now we need to select all these checkboxes one by one so to select this I'm going to write a looping statement it can be enhanced for
14:31
Speaker A
So now we need to find one common attribute for these input tags. This is one input tag, this is another input, this is another input, and so on. So totally seven inputs are there.
14:44
Speaker A
especially when you're working with these type of scenarios try to use always enhanced F Loop instead of using normal F Loop okay so I will show you both but always try to prefer enhanced F first let us start with the normal F
14:58
Speaker A
So for every tag, we need to find one common attribute. And if you look at here, what is the common attribute which we have here? Class, type, we cannot use. Okay, type is a checkbox, okay, that is also fine.
15:14
Speaker A
have to repeat this depends upon the number of checkboxes so how to know number of checkboxes how we will know number of checkboxes total number of checkboxes in this size so here I'm directly writing I less than checkboxes do size so depends
15:35
Speaker A
But before that, if you look at straightforward class, most of the times you will prefer to use, right? Class form-check-input, class form-check-input, class form-check-input, form-check-input.
15:50
Speaker A
item from the checkbox what is the method to extract the value from the checkbox get so dot get off get method will take index so I is a index I'm passing I value so this will return the First checkbox Once it is
16:07
Speaker A
So for every checkbox, the class attribute is exactly the same, right? So now I'm trying to use this class attribute to locate the element along with the XPath.
16:23
Speaker A
second round I value becomes one then again another checkbox you will get here and then again you are clicking so this will repeat for all the checkboxes which are captured here so this is a one way to select all the check boxes so let us
16:40
Speaker A
Okay, so for example, if I say, okay, so let us see only class name. Okay, so let me capture the class name. I'm writing only class.
17:13
Speaker A
check boxes because here we are not going to use any index checkboxes take one by one each checkbox and store into variable Here checkbox and Now tell me what is the type of this variable type so from the
17:31
Speaker A
So this is a class. When I use only class name, if you want to use CSS selector, you have to put dot, right? See, nine elements are matching.
17:47
Speaker A
element right now we are directly getting the web element here so once you get a web element directly perform click action okay you can directly perform click action C here we not getting any index we are not getting any specific
18:03
Speaker A
Suppose if you're using an XPath also, let's say //input tag and here I'm using class equal to form-check-input.
18:18
Speaker A
selected so this is the simplest way uh to select all the checkboxes using enhanced for Loop okay done now so let us see one more and commenting this again one more scenario so these two scenarios most commonly do everybody know most of them
18:50
Speaker A
Okay, form-check-input. Now can just observe nine elements are matching, but how many elements we required? Only seven elements.
19:15
Speaker A
three checkboxes So based upon the input so I want to select last three checkboxes let us assume you have 10 check boxes or 20 check boxes I don't know how many are there but from the last I want to select two let's say
19:30
Speaker A
But it is matching nine elements. You can see this XPath is also matching with the radio buttons. Can see this XPath is also matching with the radio buttons along with the seven elements. These two elements also matching, but we don't need these elements. Only the seven elements we required.
19:46
Speaker A
from the 7 six and five but if you are increasing like this suppose you have 10 Jack boxes how will know what are the last three checkboxes you don't know right so you need to dynamically find last checkboxes three
20:01
Speaker A
So if you write XPath like this, okay, if you writing XPath using a class, it is not exactly matching with seven elements. It is also matching with some other elements. So now we need to filter these.
20:25
Speaker A
accordingly you can do it here uh we have to use only normal F Loop okay enhancer for Loop we cannot use because we want to deal with the index here so how to select last three checkboxes just observe here I have multiple checkboxes
20:44
Speaker A
one 2 3 4 five six 7even totally seven check boxes indexes start from what zero right so this is zero 1 2 3 4 5 and six these are indexes now I want to capture only last three checkboxes I want to select last
21:07
Speaker A
three checkboxes okay to select the last three checkboxes first of all we need to count total number of checkboxes how many checkboxes are there total number of checkboxes 1 2 3 4 5 6 I'm not talking index okay 1 2 3 4 five six seven
21:26
Speaker A
checkboxes are there so total is seven checkboxes minus how many check boxes you want to select from the last three checkboxes one two three check boxes say 7 Min - 3 equal to what 4 okay 7 - 3 4 now this
21:44
Speaker A
four is a starting index from here we have to start reading the checkboxes so from here we have to start four five and six okay three check boxes you will get suppose I want to select last uh two checkboxes only five and six I
22:03
Speaker A
want to select so how to get these two check boxes total number of checkboxes minus how many checkboxes you want to select only two checkboxes so 7 minus two equal to five is a starting index so need to
22:18
Speaker A
start from the five so then five and six you will get only two Che boxes so this is the formula so total number of checkboxes minus how many checkboxes you want to select that will give you starting index okay and in the looping statement
22:35
Speaker A
starting index should be this one not zero okay so let us try to apply this formula so here I'm writing one F Loop so int I equal to okay uh I will write here formula so this is a Formula so total number of
23:03
Speaker A
checkboxes minus how many checkboxes you want to select equal to starting index all right so where we have to start index I equal to four okay and then I less than or equal to checkboxes do size and then i++ so starting in it should be
23:32
Speaker A
four now how to select checkbox from the checkboxes because we already collected all the check boxes from this we are getting the element by using get of I dot click so this will able to click right so now execute run as Java
23:53
Speaker A
application so this technique will be very very useful if you have more number of checkboxes yeah now you can see the last three check boxes have been selected so this is the formula so total number of checkboxes minus how many checkboxes you
24:10
Speaker A
want to select from the last that will give you starting index equal to starting index that you have to use it here okay now I want to select first three check boxes next scenario select first three checkboxes so then how can we do
24:39
Speaker A
it first three check boxes yeah we don't have any formula we just start from the zero itself very simple so for first three check boxes right we'll start I integ equal to zero and if you want to select first three checkboxes
24:59
Speaker A
what could be the condition here only first three check boxes mean 0o one and two right 0 1 and two that means less than three less than three you can put or else I less than or equal to two also
25:14
Speaker A
you can put because it is Count from zero we need zero we need one and we need also two so 0 1 2 whenever I value becomes three condition become false so I can put less than three and then I ++
25:29
Speaker A
so when I say less than three it will select first three checkboxes now from the checkboxes dot g of I dot clicker so this will select first three checkboxes and you need to just change the uh condition accordingly you can
25:54
Speaker A
select something wrong sem okay now first three checkboxes now another requirement this is the fourth okay now another requirement I want to unselect checkboxes so some checkboxes are already selected but I want to unselect and at the time of unselecting
26:43
Speaker A
whichever checkboxes are already selected I want to verify them and if it is selected then I will uncheck or unselect got my point so you may have multiple checkbox 3 4 5 6 7 and some of them are selected
27:03
Speaker A
like this randomly or sequentially some of them are already selected and whichever checkboxes are got selected I will unselect only those checkboxes I will unselect only those checkboxes okay and uh if I use click method directly this will go and select
27:24
Speaker A
every checkbox and suppose if everything is got selected if I use again one more time click method this will unselect every checkbox but that's not my requirement I want to unselect first thing first step I will select a few checkboxes randomly after
27:42
Speaker A
that I will SE unselect only those checkboxes whichever is already selected only those checkboxes I want to unselect okay then how it is possible so this is my requirement whichever checkboxes are selected just I want to unselect okay so what I will do is I
28:03
Speaker A
will try to uh repeat this Loop one more time fifth requirement un select checkboxes if they are selected based on the condition so first step what I will do is I will select first three checkboxes or randomly you can select
28:25
Speaker A
whatever checkboxes you want so this particular Loop group we'll select checkboxes okay and uh now I want to unselect in this you can use normal for Loop or enhanced for Loop anything is fine so I'm trying to use enhanced for
28:41
Speaker A
Loop directly checkboxes I'm taking checkboxes and from this I'm reading each and every checkbox one by one and this is a web element and this element I'm just going to click so this particular Loop will select all the
29:04
Speaker A
checkboxes this Loop will select all the checkboxes okay suppose if you want to select only first three checkboxes how can we select then we have to write only normal for Loop so through enhancer for Loop we cannot select a specific
29:18
Speaker A
checkboxes because we don't have indexing here index concept we won't follow here right so if you want to work with the specific check boxes randomly or dynamically then you have to write only normal for Loop enhanced for Loop will not work okay let's change it
29:36
Speaker A
to the normal F Loop so this will repeat this will check first three check boxes okay now execute okay first three is got selected so now what I will do is I will pass my code sometime because it will go very
30:03
Speaker A
faster you cannot see this I will just pass for five seconds and after that I will write the same Loop one more time okay same looping statement first time this will select all the checkboxes and after that it will un
30:22
Speaker A
select all the checkbox all means three checkboxes okay now but here I don't want to uh unselect everything let us try to execute first so here I put three only right so that's the reason only three got selected here I will put checkboxes
30:41
Speaker A
do size okay just observe what will happen so initially by using first Loop I'm selecting only three checkboxes but in the second for Loop I'm considering all the checkboxes see first three check box boxes are selected after 5 Seconds see here first
31:04
Speaker A
three checkboxes are unselected but rest of the checkboxes are got selected but I don't want to do like this whichever checkboxes we selected initially I want to unselect only those checkboxes only these three checkboxes I want to unselect not rest of them so what we can
31:21
Speaker A
do here before clicking on this checkbox first we will see whether it is selected or not if it is selected then click on it then unselect it otherwise no so how can we put the condition now if this checkbox so this will give
31:40
Speaker A
you checkbox right checkboxes get of I dot is selected we have conditional method right is selected and then do this if it is already selected then do this otherwise don't do this now execute So based on the condition we can we will
32:03
Speaker A
know exactly whether it is selected or not if it is selected then unselect so now first three are selected and after that it will unselect only those three checkboxes okay so this is how we can handle checkboxes based upon different type of
32:24
Speaker A
requirements okay but in the radio buttons we don't have such type of choices so in the radio button we can select only one radio button at a time if you select another radio button first one will be removed in the last classes
32:37
Speaker A
I have shown you already how to handle radio button so you can check it okay so is this clear everyone so how to handle checkbox so these are the different scenarios you have to remember uh in interior perspective it is very very
32:56
Speaker A
important right now the next element how to handle alerts which is also called as popups and most important listen carefully alerts or popups sometimes when you are uh working on web web pages or web applications you can see some kind of pop-ups or some
33:23
Speaker A
kind of alerts until unless you close those alerts or popups you cannot proceed forther you have to close them then you have to continue further so how can we handle such type of alets in selenium web driver okay so in selenium uh web
33:43
Speaker A
basically there are three kinds of alerts are there there are three kinds of alerts so let me show you what are those types of alerts okay so I'm directly opening the page so these are also called as JavaScript alerts so these are the three
34:11
Speaker A
alerts uh first one is just like a normal alert if I click on this button you will get the normal alert with okay button this is just having only okay button so this is a normal alert and uh when you click on second
34:26
Speaker A
one this is called confir information alert means we it will have two buttons okay and cancel we can click on one of these button this is called confirmation alert third one is prompting alert when you click on this you will see one input
34:42
Speaker A
box along with okay cancel button this is called prompting alert okay so one is normal alert confirmation alert and prompting alert so now we'll see how to handle this type alerts until unless you close this you cannot do any other operations
35:01
Speaker A
on this web page and if it is a confirmation box we have a two options either we have to close this by using Okay button or we can close this by using cancel button and uh in the prompt
35:13
Speaker A
alert again again we have input box if you want to pass some text in the input box we can pass it and we can also capture the text from the alert window and we can also click on okay button or
35:23
Speaker A
cancel button we can close this by using either okay or cancel button so options are there so this is called prompt alert these are the three kinds of alerts okay so to handle this alert window how to handle in web drive so
35:39
Speaker A
first thing uh we need to remember alert is not web element so when you click on this you will get this window right this is not a web element and you cannot inspect this button you cannot inspect this label
35:55
Speaker A
anything so if you try to right click on this you will not get get anything you can't inspect these things because this is not web element and even if you go back and see prompt alert this is also not web
36:09
Speaker A
element this entire alert is not web element so inside this alert inside this elect box whatever elements are there we cannot inspect those elements we cannot get any attributes of those elements okay this is the first thing you need to remember alerts windows are
36:26
Speaker A
not web elements not not only alert windows inside the alert window you can see any elements they are not web elements because we can't inspect them we cannot get any attributes for those elements that is the first thing now so
36:41
Speaker A
how can we handle this so there are specific methods are there in web driver and by using those methods we can handle alerts now we'll see one by one I will show you all three boxes one by one listen carefully so I'm
37:00
Speaker A
creating a new class andle alerts right so now we created driver instance and then launching the page and then maximizing it now let us deal with the first type of alert so normal alert with okay button normal alert means which is
37:43
Speaker A
having only okay button one single button okay so before getting alert window we have to click on this element so once you click on this element then only you will get this elect window right so let us do these
37:56
Speaker A
are web elements again so these buttons are web elements so we can easily inspect them so we can just inspect and if I go back to the selector Hub you can see the xath for that element use this
38:15
Speaker A
xath so we are finding single element so I can say driver. find element by.
38:24
Speaker A
XPath and do click so we have to perform click ction on that element that means it's a button right so perform click action so once you done this action this will open the alert window okay so once you open the alert
38:39
Speaker A
window what is the next action is we have to just close this right just we have to close this how to close this if you want to close this manually what you will do you'll click on the okay button
38:51
Speaker A
once you click on the okay button it will close but in web driver we can't inspect them so we cannot consider this as a element so we cannot apply click method so what we can do is whenever you see
39:07
Speaker A
alert window you have to switch your driver from this particular page to this particular alert we can switch it okay so how to switch driver dot switch to dot alert this is a command so what this command will do is whichever alert
39:28
Speaker A
window is currently open on the web page this will capture that alert window that we can store in a variable also if you want okay so this will go and switch to the alert window and immediately you can
39:46
Speaker A
call one method called accept this is a method so there are two commands are combined here one is switching to the alert till here it will switch to the alert or it will return the alert window once you get an alert
40:01
Speaker A
window we are calling accept method so this will close alert window automatically okay just observe once I click on this element I just put some weight statement so that you can clearly see that otherwise it will go very
40:15
Speaker A
faster we cannot see p to 5,000 milliseconds it is not because of synchronization problem I'm just keeping this time just for passing the code so that once you open the alert you will see whether it is there or not and after
40:30
Speaker A
that also you can see so after clicking on this I'm closing the alert window just observe here when I run this as a Java application so there are two commands are combined here so it is launching the alert window now we can see after 5
40:50
Speaker A
Seconds it will automatically closed okay so you can see you successfully clicked on alert so we successfully close alert by so this is a command actually driver.
41:04
Speaker A
switch to. alert this this command will return alert window once it returns alert window we are accepting it okay suppose in alert window I want to do some other action suppose I want to capture this message and then I want to
41:19
Speaker A
close this okay so then we want to perform multiple operations so whenever you want to perform multiple operations don't directly use this command so try to save that or try to store that alert window in a variable I call it as let's
41:36
Speaker A
say my alert okay store this alert window whichever alert window this command is returning store that alert window in a variable now what is the type of this variable so this will return alert type of variable you can see this is the
41:55
Speaker A
return type alert object it will return so the type of variable should be alert it is not Boolean it is not web element it is not a string okay there is a special return type is there that is called alert so
42:11
Speaker A
that we have to again import from org. open. selenium so once you get this element in a variable then you can do multiple operations you can get text from the web element you can get the text from the alert by using get text so
42:26
Speaker A
this will give you text from the alert window so I'm directly writing here I can say system.
42:38
Speaker A
println just a second okay inside this you can write alert do get text sorry my alert. get text so get text is a method through which uh we can capture the text of the alert window so my alert do get text and
43:05
Speaker A
again if you want to close after that I want to close my alert spelling mistake okay so then I can say my alert do accept so if you want to do multiple operations like this you can store that
43:24
Speaker A
alert window in a variable and the type of of the variable is what alert remember this point okay sometimes it will be very useful so if you want to perform only single action just want to close so you can directly switch it and
43:41
Speaker A
immediately you can use accept and if you want to perform multiple operations like if you want to get the text or if you want to accept it then store that in a variable and after that use that variable here okay so the
43:55
Speaker A
variable type is what alert okay so sometimes we want to validate the text is correct or not okay so this text is correct or not how we can validate you will first capture the text existing text and you compare with
44:10
Speaker A
your expected text okay if both are matching your test pass both are not matching your test fail so that's the reason we want to capture it okay for everything there is a requirement we are not doing blindly everything okay so whenever you want to
44:27
Speaker A
f we have to capture the text because we want to Val something in this inside this okay fine so let us try to execute and see this and run as after capturing the text then it will close my alert
44:51
Speaker A
window okay it is done so go back and see yeah this is a EXA mage message which is displayed on the alert window okay so is this clear everyone how to handle normal alert means which is having only okay button nothing have
45:10
Speaker A
only okay button so first point you need to remember alert is not a web element whatever elements are there in alert window or alert box which we cannot inspect we can't get any attributes so how can we handle we have to use this
45:25
Speaker A
method driver do switch to do alert so this command will return alert and what's the type of the variable is alert and once we get a variable we can perform whichever operation you want so what is the main method we use here is
45:39
Speaker A
accept this method will automatically close alert window okay so this is how we need to handle normal alert window now we'll see another window confirmation alert so this is the first one second confirmation aler means what which is
46:09
Speaker A
having okay and cancel button so okay and cancel so both the buttons you can see on confirmation alert right so if I look at here this is also a type of alert window so this is also not a web element okay this is
46:29
Speaker A
whenever you say it that's not a web element but here we have a two choices we can close this by clicking on okay when I do this you can see you clicked on okay button and also you have another
46:44
Speaker A
choice we can close this by using cancel button when I click on cancel that is also closed but here is say you clicked cancel so in the confirmation alert we have a two chart es either we can close this by using
46:59
Speaker A
okay or we can close this by using cancel so if you want to close this by using okay you have to use accept method just like before we use accept method right and if you want to close the
47:13
Speaker A
select by using cancel then you have to use dismiss method there is another method called dismiss so when I use dismiss method that will close the select window using cancel button that's the only difference so that is the additional method which
47:29
Speaker A
we can use it here so let me click on the second button here JS confirmation button so that will open the alert window so this is a second element so here I'm performing click action so this will open prompt
47:59
Speaker A
alert and after that I'll wait for some time here thread do sleep if I don't put weight statement it will work still but it will very go very faster you can't see them right so this will open alert window now my
48:15
Speaker A
requirement is I want to close alert using accept Okay so because I'm performing single action I don't want to store them in any other variable if you still want to store you can use the same command to store the alert window another variable
48:31
Speaker A
or else you can directly do this so from the driver instance driver do switch to do alert so this command will switch to the alert window or return the alert window dot accept so this will close aler using Okay
48:52
Speaker A
button this is the one command the other command is d a. switch to. alert do dismiss so this command will close aler using cancel button okay now we'll see the second one directly in dismiss okay this is my alert window if it is
49:31
Speaker A
closed using cancel you can see you clicked on cancel so dismiss method is working fine and accept is as usual so if you want to close aler window using accept you can enable this using accept so these are the only two methods
49:53
Speaker A
you have to just remember driver. switch to. accept driver. switch. alert. accept driver. switch. alert. dismiss okay so I can say you click it okay button so these are the two methods again if you want to capture the text
50:10
Speaker A
you can use it get text method also you can use it you can store the Tet in another variable also you can use get text method so whatever you done in the previous one same thing you can repeat
50:20
Speaker A
for the confirmation message also confirmation aler also so accept and dismiss how many methods we learn so far now let's accept and dismiss so you can see driver dot switch to do alert do accept same thing driver dot switch to dot alert dot dismiss and
50:56
Speaker A
there is one more command we learn get text driver dot okay switch to dot alert dot get text you can use get text directly like this or if you want to store alert you can store this Alert in another variable and that
51:14
Speaker A
variable. get text method you can use okay these are the three methods we have remembered so this is confirmation alert now there is another type of alert which is called prompt alert okay next third type of alert prompt alert so which is having
51:44
Speaker A
input box which is having input box and you don't see more number of elements in this okay so currently there's only one input box you don't see two input box box or four input box not like that alert window doesn't have any
52:03
Speaker A
elements so this is a input box so there's only one input box along with okay cancel for okay cancel we can use dismiss or accept but how can we handle this input box suppose I want to pass some text into it I want to pass
52:17
Speaker A
something called welcome okay and after that you can just click on okay button so then you can see whichever value we have entered in the input box which is appeared here here and suppose if I don't uh if I pass some
52:31
Speaker A
value and I'm clicking on cancel here so null you're getting null null means you're not passing anything and whichever you are passing in the input box when I click on okay button that you will get so I don't have anything so it
52:44
Speaker A
is empty right so we'll see how to pass a value into the input box so first of all inspect this element so this is xath for the element so driver not find element by. x paath dot click so this will open the prompt alert
53:17
Speaker A
window now my first requirement on this is uh not closing this in uh not closing this alert window before closing we need to pass some text here but here this input box we cannot inspect we cannot get any attributes for this
53:33
Speaker A
element so how can we do this and also after passing this value I want to close this by using okay or cancel button so I want to do multiple actions so first let us capture this elect window in a
53:45
Speaker A
variable so how to capture again driver dot switch to Dot aler and that I'm going to store in a variable I say my prompt aler and what is the type of this variable obviously this is aler type and
54:08
Speaker A
now take this variable dot okay let me give simple name okay take this variable now I want to pass some text in the input box so what you can do take this aler window variable dot Direct you can call send
54:27
Speaker A
Keys method send keys and here I'm passing welcome so the send Keys method you can directly access from the alert object so from this you can directly call send Keys method so this particular text will go and provide inside the input box
54:43
Speaker A
directly and after this what you need to do you need to close this alert so take the same alert object dot dismiss or accept accept if you want to take that value in input box we have to use
54:59
Speaker A
accept Okay this is how so I'll put some weight statement again okay just observe this so we are clicking on this and then it is open the alert window we capturing into variable and then we are passing the text in
55:18
Speaker A
input box and then we are accepting it so when you perform these operations you can't see this value is ENT entering in the input box so normally when you run your automation whichever value are passing in the input
55:32
Speaker A
box you're able to see that value at the run time right at the time of execution you can see that value which is properly entering in the input box or not but here you can't directly see the value in
55:43
Speaker A
the input box but how we will know whether it is provided value or not after closing the window after closing this you can see the value here okay if you're able to see the value here that means you have successfully provided
55:59
Speaker A
value into the input box that is a confirmation okay let's try to execute now see here here you can't see the values providing in input box or not but after closing it you can see the value here okay so this is another thing
56:27
Speaker A
because that's not a web element actually so this is how we can handle prompt alert and if there is an input box you can directly get this element and you can directly pass send keys this is another method driver dot switch to do
56:48
Speaker A
alert do send keys and here we can pass some text so all the methods we can access from driver. switch. alert but if you want to do multiple operations better to store Alert in a variable suppose instead of
57:03
Speaker A
repeating this for everything can store this Alert in a variable like this my alert and the variable type is what alert okay and then you can use this object everywhere like this instead of writing same thing multiple times store
57:23
Speaker A
that Alert in a variable and refer that variable for every type of action so these are the actions accept dismiss get text and send Keys okay so these are all different methods are there to handle alert windows so normal
57:43
Speaker A
alert then we have a confirmation alert then we have a prompt alert and uh apart from these three sometimes we can see authenticated popup as soon as you open the website it will give you username and password that is
58:01
Speaker A
also type of popup or alert so we'll see how to handle this later but for now these are the three alert Windows now I'll tell you one more thing in interview people will ask you so how to handle alert window so you'll say driver
58:19
Speaker A
dot switch command is there so switch. alert that will return the alert window and then we will perform the different type of of operations right so this is a command will give you alert window and then we can perform the different
58:32
Speaker A
operations but my question is I don't want to use switching command I don't want to use switching command then how can we handle alert if you don't want to use switching command how to get the alert window in a
58:49
Speaker A
variable how can we perform these operations so most of the people don't know this so these type of questions you will get in interview so by using switching command we can easily handle all types of alert windows but without
59:04
Speaker A
using switching command how can we handle this okay so here there is another option available using explicit weight using explicit weight also we can handle alert weight using explicit weight without using switching command I will show you practically just observe this
59:26
Speaker A
is type questions you will get in interview most of the times tricky questions okay handle alert without using switch or we can say handle alert using explicit weight okay using explicit weight I'm just going to handle alert window that
59:52
Speaker A
means without using switching command these are two concepts here handh alert without using switch to do alert that means uh by using explicit weight we can handle okay so just observe so I'm taking same example go back to the previous
60:23
Speaker A
one it can apply with any type of alert problem so I'm taking this one all right so this is the driver instance and launching the my browser and maximizing the window and clicking on the button after waiting some
60:56
Speaker A
sometimes so we are getting the alert window here import this alert and then we are performing get text and accept everything is same okay now I don't want to use uh the switching command and without switching command we
61:14
Speaker A
want to handle this so normally when you run this it will able to get the text from the alert window and then we are able to accept yeah okay working fine now I still want to get alert window like this but I don't
61:37
Speaker A
want to use switching command here okay so then how can we do this remove this so instead of doing this we want to use explicit weight so how to use explicit weight first there are two parts right declaration and
61:54
Speaker A
usage so as soon as you created the driver first declare the explicit weight how to declare web driver weight do you guys remember this web driver weight my weight equal to new web driver weight and here we have to pass driver
62:16
Speaker A
instance along with the time duration dot of seconds Max 10 seconds I'll put this is the Declaration of explicit weight right so just import this this is a declaration of explicit weight so explicit weight now we want to use this weight
62:46
Speaker A
for the alert okay how to use it here after opening alert window we want to wait for the alert okay that is a condition explicit weight is for what based on the condition right so after clicking on this for we want to wait for
63:03
Speaker A
the alert once aler is waited then we can get it so how to wait for the aler we have a method here what is that uh take this variable my weight my weight do until until expected conditions Dot
63:26
Speaker A
is alert present this is a method alert is present okay this particular method this particular condition becomes true whenever an alert is open okay and once alert is open this will return alert into a variable so that I'm going to create my alert the
63:49
Speaker A
type of the variable is alert again same process and this alert you can use this is alert variable we are referring here and also here so this is another way my weit do until expected conditions do what alert is present after clicking on
64:08
Speaker A
this this will wait for an alert so once the alert is available then it will capture it it condition becomes true as soon as the condition becomes true then it will return the alert window in a variable and then we can continue
64:24
Speaker A
further operations this is the another way to capture the you got my point so this is a another way so by using explicit weight we can also capture the alert window now execute and see okay now alert is open now it is
65:00
Speaker A
successfully closed so if there is no alert window alert not found exception you will get if there is no alert window alert not found exception you will get okay so clear everyone so this is how we can handle alerts now
65:34
Speaker A
I'm going to show you one special type of alert or popup which is called authenticated popup most of the times we call then and Inter prospective which is most important scenario so that's the reason I told you here by using explicit
65:49
Speaker A
weight also we can handle alerts okay now let's move on to the next one so there is another special type type of popup which is called authenticated popup or we can say alert so what is the specialty of
66:05
Speaker A
this let me show you an application so it's very difficult to find these type of applications so normally when you're working on realtime environment uh if you're are able to access real applications until unless you provide your uh username and
66:23
Speaker A
password you can you can't access any applic UI so you cannot directly see the website so before opening your website it will ask you your authentication so once you provided your authentication then only it will allow us to see all
66:39
Speaker A
the information so if I look at here this is a different type of page just observe I'm I'm just opening the URL press enter see here this is called alert or popup and this time we have a user name
66:56
Speaker A
and password okay so this is called authentication popup until unless we provided username and password we cannot directly sign in we cannot access okay if you provide username and password manually say admin admin I'm passing username and password admin admin click
67:16
Speaker A
on sign okay now you got a proper message congratulations so if you get this message login is success sucessfully done so this is called authentication popup so how to handle these type of popups and again just like a normal
67:37
Speaker A
alert the popup is also we cannot locate that is not web element now the input boxes which are there inside the popup we cannot inspect we cannot directly pass user and password into them okay so this is a special type of
67:55
Speaker A
popup so how can we handle this no this is a different type of it is not a prompt alert actually this is called as authenticated popup okay so that is not part of your web application that comes from the
68:14
Speaker A
browser itself so we cannot handle this but there is a way to handle this cannot handle means what we can't inspect those elements that's the first thing we cannot get any attributes for those elements second thing we cannot use the
68:29
Speaker A
switching command also we can't use the switching command also switch command will not work and even explicit weight also won't work so what we can do is let me show you so the username and password we have to pass along with the URL that process
68:52
Speaker A
is called injection process we have to inject username and password inside this URL itself then we can avoid that particular authenticated popup so we cannot handle that directly but we can skip it we can avoid it okay so how can we avoid it when you
69:16
Speaker A
pass username and password along with this URL then we can avoid it okay so how to pass username and password along with this URL a small technique is there let me tell you so this is the user this
69:29
Speaker A
is the actual URL so we need to inject username and password into this URL so we have to follow one syntax actually the syntax is HTTP col SL slash username colon password at the rate after that you can
69:51
Speaker A
pass this entire thing this is a syntax so here in this particular place we have to specify username and password along with anator so this is a syntax so username and password we directly pass along with the URL so now let us create
70:09
Speaker A
a URL with the username and password and then we will see whether it is asking username and password so here instead of username I'm passing admin here password is also admin okay now I'll try to open this URL
70:24
Speaker A
on the browser so previously when I open url directly it is asking for username and password right now I'm passing this URL along with username and password see here it is just skip that window and directly it is login and it
70:41
Speaker A
is giving congratulations because we already passed username and password along with the URL so it is automatically skip that authenticated so this particular process we have to follow so go to eclipse and let me create another example this is a process of injecting
71:02
Speaker A
URLs injecting the user information along with the URL okay create new class handle authenticated for yes that is a common syntax in all types website and before that you can check okay whether is authenticated window or sometimes it
71:30
Speaker A
will be normal frame you can still locate an elements and you can still perform the actions in those cases you can do like that you don't need to follow this process okay it's very rare case and also it's very difficult to
71:43
Speaker A
find this type of application in the Internet only in the real time environment you can see these type of things very rarely you can find this okay now observe this okay now just observe I'm directly launching the page without passing
72:06
Speaker A
username and password see you able to see the sign in form so this we can't directly handle switching command will not work inspecting elements also will not work but sometimes in your application it may be a simple form you have to cross check
72:30
Speaker A
okay whether if you're able to inspect these elements you can continue with normal way locate this element using send geys method then click on the sign in button that is a normal way if you're not able to recognize this then you have
72:43
Speaker A
to follow this approach okay now you're getting this so instead of passing this URL I'm trying to pass this modified URL along with username and password okay I can pass this driver dot get off like this so which contains the
73:04
Speaker A
username and password so once you run this it will skip that window and directly login into the application okay done so just remember these two okay this is a syntax so this is simplest way to handle authenticated popups so switching command will not
73:44
Speaker A
work weight command will not work normal attributes will not work so only we have to inject the username and password along with the URL Okay so this is all about alerts today's session and tomorrow session we will see some
74:02
Speaker A
different frames and how to handle different type of Windows all that all the stuff so this is the topic for today's session and uh as part of assignment you guys can try this so in the test automation form so you can see uh there are some
74:26
Speaker A
elements are there so I already shown you this and if you scroll down you can see some checkboxes in the table here you can see some checkboxes are there right so just try to select this can just try to select this
74:42
Speaker A
checkboxes okay this you can try this one type of assignment and there's another uh assignment window based popup is also same process you can access same process suppose uh okay so I'm trying to open this okay I'm not passing email address password just
75:08
Speaker A
click on the login here so you got one alert window so this also you can handle so all kinds of popups you can handle only through switching command so driver. switch to. alert do accept so that will close all kinds of
75:28
Speaker A
popups or window based popups all kinds of popups you can close this is applicable for all types of popups so you guys can try this even if you click on this I'm not passing anything here just click on the login you will get this and
75:46
Speaker A
provide some email address and click on the login it will give you this message this not pop up okay so don't provide anything here just click on login here then you will get this so these type of apart from this when you are working
76:00
Speaker A
with any other web pages normally you will see some popups okay in that case also you can handle those type of popups and uh popups in the sense they're not ads okay sometimes in your web page you can see some ads randomly
76:17
Speaker A
right from AdSense so they are different they are not popups so Those ads if you want to disable you have have to use Chrome options and we have to disable the ads that I'll show you later ads are
76:31
Speaker A
different alerts and popups are different alerts popups are purely belongs to the your application but ads are not belongs to your application so the ads are randomly displayed on the web page okay so this command switching commands Works only with alerts and
76:49
Speaker A
popups they're not for ads ads are totally different okay so how to disable ads and how to run your test in Indigo mode and also how to uh run your test on the browser with the selector app suppose sometimes
77:07
Speaker A
when you run your test you don't see these plugins currently you're able to see all these plugins right I have added on my Chrome browser but when you launch your web page through automation you can't see any plugins so if you want to
77:19
Speaker A
run the test using plugins how can we do this these are all things we can do by using Chrome options class there is a special class is there I will discuss this in the separate class okay then we
77:29
Speaker A
will see all these things so the switching command is only meant for what handling alerts and popups so for uploading documentation everything we have to use some third party tools like Auto it sigi because web driver is purely web-
77:50
Speaker A
based automation tool we cannot automate desktop things okay so if you files are available on your desktop you canot directly upload through web driver commands so we have to use some third party tools if you don't want to use third
78:06
Speaker A
party to tools still you can handle this you can directly pass that path in the box and that is also another class I'll show you that how to upload the file how to download the file so we'll discuss
78:17
Speaker A
that in the next classes coming Classon so all these topics are our future topics guys don't ask me now so we will have a separate sessions for them there still lot of things are there you can just see the curriculum once and you
78:30
Speaker A
will understand what are the topics we are going to cover okay right so let us stop here for today's session you will try this uh assignment for today and tomorrow we'll discuss some more things you can also handle radio
78:53
Speaker A
buttons check boxes from the page and this is for alert okay so that's all for today's session we'll stop here tomorrow we'll continue
Topics:SeleniumJavacheckbox handlingradio buttonsXPathSelector HubWebDriverautomation testingSDETQA

Frequently Asked Questions

How do you select multiple checkboxes in Selenium with Java?

You can select multiple checkboxes by locating all checkbox elements using a common locator like XPath and then iterating over them to perform click actions.

What is the difference between checkboxes and radio buttons in Selenium?

Checkboxes allow multiple selections simultaneously, whereas radio buttons restrict selection to only one option at a time.

How can Selector Hub help in Selenium automation?

Selector Hub helps by providing an easy way to inspect web elements and generate various locators like XPath or CSS selectors, making element identification more efficient.

Get More with the Söz AI App

Transcribe recordings, audio files, and YouTube videos — with AI summaries, speaker detection, and unlimited transcriptions.

Or transcribe another YouTube video here →