Session 36 – Selenium with Java | Keyboard Actions | Sl… — Transcript

Learn how to handle slider elements in Selenium with Java using keyboard and mouse actions, focusing on capturing element location and drag operations.

Key Takeaways

  • Sliders are controlled via mouse actions since sendKeys is not applicable.
  • Knowing the slider's current position is essential before performing drag operations.
  • Movement is based on coordinate offsets rather than fixed target elements.
  • Horizontal sliders move along the X-axis; vertical sliders move along the Y-axis.
  • Selenium's getLocation() method provides the necessary coordinates for slider manipulation.

Summary

  • The session covers handling slider elements in Selenium automation using Java.
  • Sliders are web elements manipulated primarily through mouse actions like drag and drop.
  • Unlike typical drag and drop, sliders often lack a clear target element, requiring coordinate-based movement.
  • The importance of capturing the slider's current location using the Point class is emphasized.
  • X and Y coordinates are used to determine the slider's position and movement direction.
  • Horizontal sliders require adjusting the X-axis, while vertical sliders require adjusting the Y-axis.
  • The getLocation() method of WebElement returns the slider's current position as a Point object.
  • Practical examples include moving the slider by calculating the required offset on the X or Y axis.
  • The tutorial highlights the difference between handling input boxes with sendKeys and sliders with mouse actions.
  • Understanding coordinate systems and element positioning is crucial for automating sliders effectively.

Full Transcript — Download SRT & Markdown

00:04
Speaker A
So in our previous class, we have seen how we can perform different types of mouse actions, like how we can do mouse over, right click, double click, drag and drop, and so on.
00:23
Speaker A
and which we can handle only through Mouse operations or Mouse actions and you can see the sliders most of the times in e-commerce applications like when you're selecting the price uh you can just drag and drop the price options like minimum and maximum
00:38
Speaker A
So today, we'll see how we can handle a slider element. A slider is a web element which we can handle only through mouse operations or mouse actions.
01:04
Speaker A
the value in input boxes but we can directly handle this by using send Cas but I don't want to touch these two elements I just want to handle only this one this is a slider element okay and this is also a kind of a dragon drop but this is something different okay because here we
01:24
Speaker A
You can see the sliders most of the time in e-commerce applications, like when you're selecting the price. You can just drag and drop the price options like minimum and maximum.
01:42
Speaker A
want to drag somewhere else but there is no target here so we can put here or we can put here we can put here here somewhere you can put in this range but there is no specific Target only the source is there and if you if I look at this one then we reduce this slider
02:02
Speaker A
And so, we'll see how we can handle this using automation. So let me show you a sample slider. Now we can see this page. This is a price range slider, and you can see we can just increase the price like this, and we can decrease it like this, right?
02:37
Speaker A
this okay so before handling this we need to understand something so here in this slider we we know only source elements but we don't have a Target element but still we can do the drag and draw but uh if you don't know the Target right so how we can do this so for example if I take this
03:00
Speaker A
So we can directly pass the value in input boxes, but we can directly handle this by using send keys. But I don't want to touch these two elements. I just want to handle only this one. This is a slider element, okay?
03:20
Speaker A
so we can measure the location of the element in the points in the mathematics we have an xaxis and y axis right suppose if you have something in the web page if you want to capture the location that location will be captured in the form of Point object point is a again predefined class
03:39
Speaker A
And this is also a kind of drag and drop, but this is something different, okay? Because here we have only source but we don't have a target.
04:13
Speaker A
a 0 0 point right so this is 0 0 and first one is X second one is y axis so if you want to suppose uh I put some point here or some element here so there will be some y axis and there will be
04:29
Speaker A
So normally, if you want to do drag and drop, we have to know the source element and target element, from where to where we have to drag and drop. But here, suppose this is the element starting point zero. Now this is a source element, but I want to drag somewhere else, but there is no target here.
04:44
Speaker A
element okay suppose let us say I have an element in here in the zero position now I I want to just move that element in this particular direction and which which axis we have to increase the value X
04:59
Speaker A
So we can put here, or we can put here, we can put here, somewhere in this range, but there is no specific target. Only the source is there.
05:15
Speaker A
2 5 10 100 so if you want to move this element in this direction we need to increase the x-axis value okay and sometimes you will see the vertical sliders vertical means it will be like this and here the starting point here this is another point you can move this in the down downward Direction
05:38
Speaker A
And if you look at this one, then we reduce this slider, and this is also a source, but we don't know what is the target and where we have to move is not clear, just okay.
05:57
Speaker A
this element in this particular direction x-axis we have to change and same way if you want to move this element in this direction that is also xaxis because this is a horizontal slider if you are seeing like vertical slider then we have to move y AIS x-axis is constant but if you want to move
06:19
Speaker A
Can you hear me now? I think I don't have any issues. Okay, so this is a slider element. Now we'll see how we can handle this.
06:39
Speaker A
location and this is the minimum Point according to the element design okay so first thing is we have to know where exactly this element is in this particular page maybe this element is not in the 00 position somewhere because the page in the sense from here from here the coordinates
06:58
Speaker A
Okay, so before handling this, we need to understand something. So here in this slider, we know only source elements, but we don't have a target element. But still, we can do the drag and drop.
07:19
Speaker A
we have to increase the x- axis like how much we have to increase the depends upon our requirement so where you want to move so if you want to stop here you have stop here if you want to stop
07:30
Speaker A
But if you don't know the target, right, so how can we do this? So for example, if I take this one, this is the element. So first, we need to find out the location of this element, where exactly this is located.
07:48
Speaker A
it is moving or not so this is a concept so the main thing is we need to capture the location of the element let me show you so if you know the mathematics it's very simple calculation so first
08:04
Speaker A
And once you know the location, then we can move to this target location. How can we do that? How can we get the location of the element or how can we capture the location of the element?
08:23
Speaker A
this new package day 30 Bic it's very easy very simple if you see the Practical example then you will know that okay let's create new glass slider demo uh font is visible to everyone okay now we created a new web driver instance and then we launching
09:13
Speaker A
So we can measure the location of the element in points. In mathematics, we have an x-axis and y-axis, right? Suppose if you have something in the web page, if you want to capture the location, that location will be captured in the form of a Point object.
09:35
Speaker A
what X and Y coordinates we have to capture X and Y coordinates we have to capture so how to capture X and Y coordinates of this element so we will first capture this element inspect and we need to inspect this exact element we can call it as a minimum slider I can call this
09:54
Speaker A
Point is again a predefined class, and we can create an object for the point and so on. And that point value should be X and Y coordinates, the combination of X and Y coordinates, okay?
10:32
Speaker A
is a I can call it as a minimum underscore slider element and what is this element is web element again this is a web element okay now I want to capture the the current position or current point of this element because until unless you know the current position you don't know how much you want
10:55
Speaker A
So for example, let me show you something. Okay, so normally, let's say this is my x-axis and this is my y-axis, and normally this is a 0,0 point, right? So this is 0,0, and first one is X, second one is y-axis.
11:20
Speaker A
variable of web element so when I accessing any method get location is belongs to what web element Okay so minim _ slider get location this is the method and this method will return the location of the element like x-axis and y axis so let me print this so I can say location of the minimum
11:50
Speaker A
So if you want to, suppose I put some point here or some element here, so there will be some y-axis and there will be some from x-axis, okay? Suppose let's say this is 10, 20. Now 10 can be X and 20 can be y-axis.
12:28
Speaker A
of object pointer type of object means 58 and 249 58 is an x-axis 249 is a y axis suppose if you want to get them individually like I want to get only x-axis I want to get only Y axis
12:45
Speaker A
So this is a point or this is a location, or we can say this is a point of this particular element, okay? Suppose let us say I have an element here in the zero position. Now I want to just move that element in this particular direction, and which axis we have to increase the value, X or Y?
13:05
Speaker A
will return only the y axis which is 249 when you say only get X it will return return only 58 okay remember this so we need to specify so we need to capture uh how what is the current position
13:22
Speaker A
Suppose my element is here in this page. Now I want to move this element in this particular direction. Which axis we have to increase the value of? X. We have to increase like, suppose here this is 2, 5, 10, 100.
13:45
Speaker A
value current value of x-axis is what 58 so now we can increase it so minimum slider I'm handling so this we can increase like this how much value we need to increase we will add some value let's say currently it is 58 so now what I will do is uh there is a method so we need to uh drag this right
14:09
Speaker A
So if you want to move this element in this direction, we need to increase the x-axis value, okay? And sometimes you will see the vertical sliders. Vertical means it will be like this, and here the starting point here.
14:30
Speaker A
if I make it as a 158 then it will move some position right 158th position so current position is what 58 so now how we can move this we have a method called we can take this minimum slider
14:45
Speaker A
This is another point. You can move this in the downward direction. You can move this in the upward direction. Suppose if you find these vertical sliders, suppose now my element is here. Now I want to move in this direction.
15:15
Speaker A
new actions and then we have to pass a driver instance so import this actions now by using this act object I'm calling one method called drag and drop by so see these two methods are there so drag and drop method we already used if you know source and Target you can go with
15:39
Speaker A
So which axis you have to increase? Y axis. We have to increase, okay? So the main objective here is, suppose if you want to move this element in this particular direction, x-axis we have to change.
15:51
Speaker A
to go with the drag and drop by drag and drop by this is a method and here we need pass web element the first parameter minimum slider second parameter is representing x-axis third parameter is representing y AIS so which one we have to increase x-axis currently it is 58 now here
16:14
Speaker A
And same way, if you want to move this element in this direction, that is also x-axis because this is a horizontal slider. If you are seeing like a vertical slider, then we have to move y-axis. X-axis is constant, but if you want to move horizontal direction, y-axis is constant.
16:42
Speaker A
any Mouse action we should end with the perform because that will create and complete the action so once it is drag and drop to slider uh 58 is a current position I just moved it to another 100 uh axis and 58 + 100 158 nearby you will get so now I will get position one more time after
17:07
Speaker A
X-axis we have to move, increase or decrease, whatever, okay? And here if you look at this element, for this element we can increase x-axis but we cannot reduce.
17:44
Speaker A
so nearby 158 so exactly 158 we won't get most of the time sometimes you may get sometimes you may not get because this totally depends on the the screen size resolution and everything because if you open the same web application on your small small screen or small monitor
18:01
Speaker A
And if you look at this one, we can reduce the x-axis but we cannot increase because this is a maximum location and this is the minimum point according to the element design, okay?
18:21
Speaker A
158th position so when I get the location we're expecting xaxis should be 158 and also the slider should move in this particular direction okay now we'll execute and see yes now we can see when I say 100 it is just moved from here to here this much
18:51
Speaker A
So first thing is we have to know where exactly this element is in this particular page. Maybe this element is not in the 0,0 position somewhere because the page, in the sense from here, from here the coordinates, from here and here, here and here, the entire screen will be captured in the form of coordinates.
19:09
Speaker A
plus or minus 5 okay so we are expected 158 but it is rning 157 so nearby value it is returning perfectly fine so we first capture the get location will return the location x-axis and y axis and in this slider we can change only x-axis Y axis we cannot change because this
19:30
Speaker A
So in these four coordinates, we need to find out exactly where this point is present, where this element is present. That means X and Y axis we need to first capture.
19:52
Speaker A
by this is the method element x- axis and then y AIS now we'll try to handle another one so this is a one this is minimum slider and now I'll handle Maxum slider so that we can reduce the
20:12
Speaker A
So once you capture an X and Y axis, then we have to increase the x-axis. Like how much we have to increase depends upon our requirement.
21:08
Speaker A
slider so driver do find element by. X paath okay this is a x path of the element and Dot get location okay so before that I'm just going to store this element in a variable so this is web element okay now let us find the default location of the slider maximum
21:40
Speaker A
So where you want to move, so if you want to stop here, you have to stop here. If you want to stop here, so different locations. So we can put some number. We will increase x-axis number, some value accordingly.
22:05
Speaker A
execute okay still we have not moved this maximum slider just we captured the location yes now this is a location 876 249 is returned so this is the location fine now we need to consider 876 is an x-axis the current value of the maximum slider is position is 876 now I want to reduce it so how we
22:42
Speaker A
We will check whether it is moving or not. Same way, we will capture the point of this element, maximum slider element, and we will try to reduce the x-axis and we will see whether it is moving or not.
22:59
Speaker A
reduce 49 so I can put this 49 here so it should nearly become 200 because we are reducing the x-axis currently it is 49 I can reduce 49 so it will come like this little bit okay so let's say
23:16
Speaker A
So this is a concept. So the main thing is we need to capture the location of the element. Let me show you. So if you know the mathematics, it's very simple calculation.
23:42
Speaker A
sorry uh sorry not 49 so 876 is the current position so I can reduce 76 so it should nearly comes around 800 okay because y axis we should not change only xaxis we should change so current position is 876 so I reducing 76 now it should come near by 800 let's execute and
24:10
Speaker A
So first we need to capture the location of the element. So not only the slider, if you want to capture any element locator, you can do it. We have a method called getLocation.
24:41
Speaker A
move 100 okay okay sorry uh we we have forgot something here so okay when you put 100 directly here then what happens is or when you put here 76 what happens is so what is the current value of this get location is 876 so the 76 will be added to 876 it is trying to move the uh
25:31
Speaker A
So by using that method, we can get the x-axis and y-axis of the element, okay? Now let's go and see how we can handle this.
25:53
Speaker A
right so we need to say minus 76 so that's the problem we got the same value it's not able to increase when you put 76 876 plus 76 it is taking but it is a maximum right this cannot move further
26:09
Speaker A
New package, day 30, BIC. It's very easy, very simple. If you see the practical example, then you will know that, okay, let's create new class slider demo. Font is visible to everyone.
26:31
Speaker A
so when you want to reduce x-axis we say minus one when you want to uh increase we need to say just number without minus okay now both have mode now we can see yeah so after moving we got this point
27:05
Speaker A
Okay, now we created a new WebDriver instance and then we are launching the page, and then we maximize the page.
27:30
Speaker A
minus 100 is reduced it should become 776 but we got a 778 it is still F okay so this is how we can move the slider so before moving we have to capture the current position or current point
27:49
Speaker A
So now first we will try to handle the minimum slider. So the minimum, this one. Later we will see this one.
28:09
Speaker A
way we can handle the slider so how much you want to move that you need to decide according to your requirement and according we can specify some value so toggle is a different one okay so toggle is a different one I'll show you how to handle toggle in a different case so not here
28:31
Speaker A
So first step is what we need to capture the location of this element. Location of the element in the sense what? X and Y coordinates we have to capture. X and Y coordinates we have to capture.
29:01
Speaker A
okay so now we'll see how we can handle keyboard actions using actions class so from the last class we were discussing about actions class and which are related methods and this dag and drop by is also one of the method which is
29:17
Speaker A
So how to capture X and Y coordinates of this element? So we will first capture this element, inspect, and we need to inspect this exact element. We can call it as a minimum slider. I can call this as a maximum slider.
29:41
Speaker A
keyboard actions we can do is shortcut keys normally we use l we can press enter we can sometimes we press space bar contrl c contrl v crlf contrl s so these are all shortcut keys normally we use from the keyboard so same thing we can do using actions class
30:01
Speaker A
So I'm inspecting the first one, minimum slider, and go to selector H and get the locator for this. This is the locator of the first one, minimum slider. Copy this is pointing.
30:23
Speaker A
okay and if you want to know the length for example from here to here how much length it is like x-axis length so that between that length you can put right suppose this is a starting position how much you how much maximum how much you can move it how can
30:47
Speaker A
Okay, so I can say driver.findElementByXPath. This is the XPath I have taken. This is a, I can call it as a minimum_slider element.
31:05
Speaker A
the maximum slider if you do substraction maximum minus minimum that will give you the maximum length of this x-axis and between that x-axis you can move whichever point you want so that is a simple logic you can apply shall we move to the next one keyboard actions
31:35
Speaker A
And what is this element? It is a WebElement. Again, this is a WebElement, okay?
31:52
Speaker A
scrolling down the page right this is called scroll bar that we can handle only through JavaScript executor so that I will show you later and we cannot use actions class method for this because this is not a web element but within your page within your application you can see
32:08
Speaker A
Now I want to capture the current position or current point of this element because until unless you know the current position, you don't know how much you want to move in this x-axis direction, okay?
32:28
Speaker A
so we'll see separately how we can handle this scroll bar the different approaches are there okay now let's move on to the next one how can we do keyboard actions so normally the keyboard actions will be what sometimes we press enter key sometimes we press Escape key
32:48
Speaker A
So we need to know the current position of this element. So what we can do is we alrea-
33:08
Speaker A
I'll show you an example so for that I'm taking one example to perform the keyboard actions so I'm taking this page and you can do this without keyboard actions also so normally you can do what is the operation I'm
33:34
Speaker A
going to do here is so there are two text areas are there so this is a a big text box and this is a very big text box normally we can we should not call this a text box or input box it is a text
33:45
Speaker A
area so if it a smaller input box we can say it is input box or text box but it is very bigger then we can call it as a text area okay so here there is one text area box and these are another text
33:58
Speaker A
area two boxes are there now whenever you launch your page by default uh you are focusing on the first box so first we need to go to the first box and here I will type something let's say I'll say
34:14
Speaker A
welcome to selenium yeah type some text by using send keys I will type some text in the first box I will locate the first box and I'll type something using send Keys method and after that that I will
34:31
Speaker A
select the text when I press contr a the text will be selected for selecting the text I say control a which is shortcut key and I want to copy this text control C then I will click on the tab key then the cursor move to the second box now here I will press control V same text
34:56
Speaker A
is got copied into the second box okay now how many shortcuts I have used from the beginning how many shortcuts I have used again I'll do count it how many shortcuts I do and how uh how many times I've done this so first time end Keys method I will use I can say welcome so from
35:19
Speaker A
here you need to count selecting the text how to select control a next contrl C next tab next contrl V now how many shortcuts totally four shortcuts first control control a control C tab control V so totally four shortcuts in these four shortcuts three shortcuts are combination
35:53
Speaker A
of two keys let's say control a contrl c contrl v these are the combination of two keys and I press tab also to shift from one box to another box I press tab also that's a single key action that is
36:07
Speaker A
single key action now four different Keys we have four different shortcuts now we'll do the same operation through our automation shortcut keys we use through our automation okay let me create new class keyboard actions okay so uh import this web
36:44
Speaker A
driver duration I put implicit we also and this is a page and then just maximizing the page okay so just observe very carefully so first we need to uh locate the only first second input box we don't need to locate because we are just copying the text in the second input box when I
37:14
Speaker A
press control V it will go automatically Tab Key will automatically focus on the second box so only the first box we need to locate because we need to provide some value in it so inspect the first box go to the first one inspect this first box you can see text area the tag name is what
37:34
Speaker A
text area okay go to selector Hub and get this element and go back so driver. find element by.
37:47
Speaker A
XPath and do send keys and here I'm typing something called welcome so this is normal send Keys method actually so once you provide the value in the send keys so then what we need to do is four combinations of the key first one what we have to do so we already enter the
38:12
Speaker A
value in the first box already we enter the value in the first box now what is the first action is control a that will select the text okay first one is control+ a if you working on back you should say command a command key a okay so I can say Ctrl control+ a what is the
38:38
Speaker A
next key control control+ C copy and the next key control+ Just Tab because we need to shift to the next next box and in the next box we say control+ V pasting the text so control a we'll select the
39:06
Speaker A
text select the text and contrl C copy the text into clipboard and Tab Key will shift to Second box and control V will paste the same text these are the four actions so now we'll see step by step
39:34
Speaker A
very easy very simple just try to understand this how you do manually same thing is apply here but only thing is we need to additionally apply few methods so before doing this keyboard actions we will create actions class object actions act equal to new actions then pass driver
39:59
Speaker A
object now import this actions okay so now we'll do the control a how we can do the control a action so take this act object act dot so normally when you do manually we are pressing the control and then pressing
40:26
Speaker A
the A and releasing the control releasing the this is how we will do manual right how we can do control action manually how we will do from the keyboard first we will press control that means we are pressing the control we are key down and then pressing the A and then we releasing the
40:47
Speaker A
control and we are releasing the a that's how we do manually right you can try once again in your case how we can perform control a is first we will press the control key by holding it we will
41:00
Speaker A
press the A after pressing a then we will release the control key then we will release the a four action we are doing so act dot we have a method called key down key down that means pressing the
41:17
Speaker A
key key down which key we have to make it down control so how we can specify that we have a special inter special class called keys from this we can access all keyboard Keys here so these are the different Keys which are available in the keyboard so which key we have to down make it
41:36
Speaker A
down control so here we can select control keys. control so first we are key down of control then what is the next key we need to make it down control a so followed by dot key down keys control dot uh we need to press c c is a what character C is a character so we need no need
42:06
Speaker A
to press the character if it is a character we no need to press so simply what you can do you send keys and put C here that's it okay so this will perform control C action dot key down we press
42:23
Speaker A
the control key dot send keys of C so whenever you press any key you can use key down and suppose if you find any characters use send Keys method so this action is still not completed key down keys.
42:40
Speaker A
control dot send keys of C Okay C is a character C is a character so whenever you want to press a character you need to use send GES method so control C then we have to release what control
42:57
Speaker A
key we have to release so what we need to do is the continuation dot key up key up which key we have to release control key we have to release so here we have to specify Keys dot control and
43:14
Speaker A
finally dot perform this is a complete statement for control a control a now just observe one more time we are pressing the control here and and then C control C action is completed then we remove the we release the control key how we can release key up so key down is for pressing the
43:39
Speaker A
key key up is for releasing the key key up which key we have to release whichever key we pressed here the same key we have to release so keys do control and uh to complete that action we need to call perform because it's a actions class object so this is a complete statement of control
44:01
Speaker A
a everybody's understood this if you understand this rest of them is very very easy so control a we are pressing the control C and releasing the control that's it understood everyone the statement is clear please confirm in the chat box is the statement clear only two methods key down
44:24
Speaker A
and key up if you know these two methods you can handle the keybo code actions only two methods key down and key up okay suppose so this is called control a this is control sorry this is sorry not
44:39
Speaker A
c a right because we want to select the text contr a will select the text now we need to copy the text into the clipboard contr C action we have to do so same thing just copy control C how we can do
44:59
Speaker A
the contr C same process control press the control by using key down and this time send key should be C and key up control. perform so what this method will do the statement will perform contrl C so here two combinations two keys here two keys now come to the third one tab tab is what
45:28
Speaker A
single key tab is a single key then how we can handle this tab is a single key so if it is single key simply we can do act dot key down key down Keys dot which key tab so we need to select the
45:48
Speaker A
tab from the do auto s keys do Tab Key down and once you key down we need to immediately release Dot dot again key up immediately key up and same key keys dot Keys dot Tab and Dot perform that's
46:09
Speaker A
it because here we don't have a two combinations so character is not there only tab so we just pressing the Tab Key here first key down and here we are releasing the Tab Key here we are pressing the Tab Key here we releasing the Tab Key now this will shift to the second box this will
46:32
Speaker A
shift to the second box now next one paste the text control V how to do the control V same same method Cy this key down control and send keys of what V because we should say paste right control V key up control dot what that's it very simple only two methods key down and key
47:08
Speaker A
up so if you want to press two combinations of keys so this is a control and character character we should always put send keys and if you want to find single key suppose I want to press enter instead of tab I want to press enter then what you should do from the keys
47:27
Speaker A
you have to take a Enter key like this so whichever keys are available in your keyboard it will show up everything so we need to take like this so four actions have completed so in the first box we selected the text by using contr a then we copied it by using contrl C
47:47
Speaker A
then we shifted to the second Box by pressing Tab Key then we copy the same text by using control B so these are all keyboard actions now we will execute the text test and then XC Java application yeah three keys also we can select I'll show you
48:09
Speaker A
that so just observe now we can see uh just a moment I think tab is not line number 32 okay so this is key down send keys of a this is C okay this is C actually okay so this is control a contrl c Tab and this is control V save it and execute one more
48:47
Speaker A
time just observe I'm not doing anything just it will pass the text in the first box by using send Keys then it will copy shift to the second box and then we'll paste it so this will take few seconds of
49:08
Speaker A
time yes can you see so the same text is got copied to the second box only through shortcut keys only through keyboard shortcut keys whatever I have done through manually same actions are replicated through automation using key down and key up so these are the only two
49:37
Speaker A
methods you have to remember for keyboard actions we have a method called key down sorry key down and key up so both the methods combinely we have to execute so second box you don't need to inspect because we by pressing the Tab Key it is automatically
50:03
Speaker A
shifting to the second box in the second box we are just saying control V okay but if you want to pass the same text manually let's if you want to type something in the second book then you have to locate the second box otherwise no need because we are doing this operation through
50:21
Speaker A
shortcut keys if you want to do you can do it through send Keys method whatever text ke is there you can capture it and go to the second box and again send keys of same value you can put it but I just want to handle this through shortcut keys that's the reason we no need to
50:38
Speaker A
loc at the second box because the tab is already shifted to the second box now cursor is waiting in the second box once the cursor is waiting in the second box when you press control V then same text will be copied in the second box itself why it is second box itself because the
50:55
Speaker A
cursor is waiting in the second Box by default so this will automat automatically happens and if you have a three keys then how we can do this suppose I have control shift a control shift a three keys I have control shift a then how we can write this command control shift a very important
51:28
Speaker A
so three keys I have now control shift a can anyone tell me first key is what control control so act dot Act is what action SL object so act dot control first we need to handle control how we can handle key down off keys. control okay we press control now immediately we press shift also
51:57
Speaker A
dot one more key down key down okay so key down and which key is dot shift right here we have to select shift so we press control and press shift then a so a is a character so here you can use
52:24
Speaker A
send keys of send keys of a is it completed is the reaction completed no so we press control and shift then we press a after that we have to release the keys control key we have to release shift key we have to release right both the keys we have to release so first we press control then
52:55
Speaker A
we press shift then a at the time of releasing also we need to go to the reverse Direction so control shift to a at the time of releasing first we need to release the shift key then we release the control key com dot key up off key up off which key we have to release first
53:22
Speaker A
shift key we have to release so whichever key we press last that key we have to releasee first so keys do shift and after that which key we have to release control dot key up Keys dot control dot perform this is a complete state for three case okay so we press control then
53:55
Speaker A
press shift then a okay after that we removed or we release the shift key then we release the control everybody has understood this example if you have a three keys you need to do like this okay suppose if you want to press enter key single key enter so how we can write the statement
54:22
Speaker A
only single key enter very simple act dot key down Keys dot enter dot immediately we have to release key up off keys do enter dot perform if it is a single key we have to do this so you can
54:50
Speaker A
put keys. spacebar keys. Escape keys do return anything keys. delete dat so whatever keys are there in the keyboard you can use all the keys by using keys class yes so the last key always release the first and uh the first key you should always release the last so whichever order we
55:13
Speaker A
followed we should go to the reverse Direction okay everybody's clear now so how often you use keyboard acction sometimes your application is allowing shortcuts because in our web application Sometimes some operations you can do using shortcut keys also if your application is
55:32
Speaker A
supporting to perform using shortcut keys then you have to test them you have to test the shortcut keys in those cases these commands will be very very helpful and if your application is allowed otherwise no need so you can do some operations using shortcut keys right
55:51
Speaker A
normally in the desktop applications also if you want to open the file what you will do this is a file right so normally we go like this using menu options and same options are available in shortcuts also so I can go to the file menu like this using Mouse operation or I can go to
56:09
Speaker A
the file menu by using control F or alt F like this right so you can test both through mouse mouse over are you able to do it or not through shortcut keys also you're able to see that option
56:22
Speaker A
or not you have to test it so that is the reason if application is providing shortcut keys then you can use this options you need to test them also because the shortcut is also should work according to our expectation okay so this is very simple example but very rare case very very rare case
56:41
Speaker A
we don't do these kind of things very frequently so very rare cases if you still have some type of requirement you can do this using Keyon and up okay so this is all about actions class so by using using actions class we can do mouse operations also we can handle keyboard actions all
57:03
Speaker A
the stuff we can handle through actions class so now we'll see some miscellaneous Concepts suppose I have uh small requirement so actions class is over now I'm just moving to the next topic first I will ask you just tell me how we can do this uh we will see through automation okay so
57:31
Speaker A
I'm just launching my page okay so there are some links here register login wish list and so on when I click on this register just observe when I click on this register registration page is open but my homepage is gone registration page is opened by my homepage is gone but my requirement is
57:57
Speaker A
I don't want to lose my homepage I still have want to have my homepage and in the next tab I want to see the registration page so if you want to do this manually how we can do this we right click on
58:12
Speaker A
the register and open link in the new tab so then you can see the registration page in the next Tab and the homepage still exist so this I want to do through automation okay so this I want to do
58:27
Speaker A
through automation once you open the registration page now we can switch to one tab to another tab because both the browser windows are open you can switch it so my requirement is instead of clicking the register on the same page instead of getting the registration page on the same page I want to
58:45
Speaker A
get this register Link in the next tab but still our driver is focused on the homepage itself now I want to switch my driver to the second page in the second page I want to handle something I want
58:59
Speaker A
to check the logo present or I want to check some element is displayed some operation I will do in the second that's my requirement but if you click on the register directly the registration page is open but home page is gone I don't want like that I want to open this register in the new tab like
59:17
Speaker A
this how we can do this so this is also a kind of a keyboard action okay this is is also context click will not work because this element is not having right click operation so when you do right
59:35
Speaker A
click you will get the options from the browser these are all not context menu options actually these are not developed by the web browser this is not developed by the application so even if you open any other page okay even if you open any other page you'll get the same options so
59:54
Speaker A
if I open any other page you will get the same options every page you will see those options okay suppose uh I'm getting another page even if you right click here you will get the same options here same options whatever you can see same options you can see even if you right click
60:13
Speaker A
here you can see the same options so these options are belongs to the browser not belongs to the application so currently this particular link is not associated with the right click functionality right click functionality not associated with this so we cannot do right click on this element but
60:31
Speaker A
still how we can open this link in the new tab like this so this is also Possible only through keyboard actions only through keyboard actions how it is possible through keyboard action how it is so normally what I'm doing through Mouse action I'm doing I'm just right clicking on this
60:50
Speaker A
register and I'm selecting the link in the new tab then it is opening in the new tab but I don't want to use Mouse operation only through keyboard action I want to do this simply what you can do is press control button press control button and then do normal click not right click normal
61:08
Speaker A
click control button and click on register see open the registration Link in the second one more time I'll show you it is a keyboard action I'm doing so press control key then Press Register link you can see the registration link is is open in the next tab okay same thing for login press
61:31
Speaker A
control key and then I clicking on the login now we can see login page in the different so this is the keyboard action not the mouse action okay now same thing I'll try to simulate just observe so new class and open the link in the new tab open the link in the open the link in new
62:08
Speaker A
tab and only through keyboard actions we can do it so I have shown you right manually how we can do through keyboard just press control and click on that link control and click on the link then link will be open in the next tab if you don't press
62:28
Speaker A
control just you click on the link it will open on the same page okay so now import this okay so now created web driver instance launching the page and Max M right so as soon as launch my page I can just find out this element uh register element inspect
62:57
Speaker A
this okay this is a element I want to click so driver. find element driver. find element by. XPath get this xath and store in the element this is registration link and this is web element okay now so I want to if I just click on this link then what happens on the same
63:35
Speaker A
page this will open registration link do click I'm directly doing clicking action so when you do direct click this will open on the same page so we lose the homepage can just observe see homepage on the same page we got the registration link
63:57
Speaker A
but I want to open this in the next tab so how we can do this first we need to press control then we need to click on the register control and then click on the register link then open the new tab
64:11
Speaker A
like this control and click on the registation link so how we can do this so instead of normal click I want to perform the keyboard action so how we can do the keyboard actions using actions class so create an actions class object object actions act equal to new actions of driver so
64:35
Speaker A
create actions class variable Now by using this act yeah now tell me how to click on the tab act dot how to click on the tab yes act dot key down keys do tab okay we press the tab key
64:58
Speaker A
after pressing the Tab Key we have to click on the link right so after pressing the tab we have to after pressing the sorry not tab control sorry it is not the tab control dot control okay we press control so once you press the control then we need to click on the register
65:26
Speaker A
control click on the register how to click on the register dot click okay dot click you can put like this keys. control do click and inside the click method we can pass this registration link okay and after clicking on the registration link we need to release the control key how to
65:50
Speaker A
release the control key dot key down Keys dot control keys dot. control do perform this is a command clear everyone so control click on that link then release the control and complete that action this is the whole statement sorry this is key
66:21
Speaker A
up okay so we we press the key here and we release the key and in between we click on the registration same manual action I just perform through automation so normally what we'll do control and click on the register that will open in the next page same action
66:46
Speaker A
I have done like this so control click on the link and release the control okay because here we are doing through actions class so the click method is also available uh in the actions class also so that's the reason here this click method is used from the actions class
67:09
Speaker A
so that the reason it is taking the element so normally The Click method will not accept element okay suppose when I'm directly using this reg link dot click so in this click he cannot pass any parameters because the click method won't accept any parameter this is a direct click on this
67:29
Speaker A
element this is a direct click and this click is a method which is accessing from the web element this click method is AC accessing from the web element class so this doesn't need any parameters it doesn't accept any parameters but here this cck method we are accessing from what this one
67:50
Speaker A
key down is returning some actions class or from the actions class we are accessing The Click meod so click method is available in two places one is in the web element class another one is action class and the click method from the web element doesn't accept
68:07
Speaker A
any parameters whereas click method from the actions class will accept parameter that is element okay now so this is a statement now let us execute and see perfect now you can see the home page is still displayed I got another page
68:34
Speaker A
okay but where is driver our driver is focused Now homepage or registration page it opens the registration page but where is our driver our driver is still focus on the homepage now I want to switch to the second page registration page then I want to perform
68:52
Speaker A
some action let's say I want to perform I want to pass some first name here how we can do that currently our driver is focused on the first homepage now I want to switch to the second page how we can do that we use switching commands Okay so this command is for what
69:13
Speaker A
control plus registration link okay so now switching So currently after after doing this operation two browser windows are open so how we can switch it switching to registration page I want to switch to registration page how we can switch it we need to first get the window
69:41
Speaker A
IDs of both parent and child so how we can get it driver dot switch to driver dot get window handles because there are two browser windows are open and this will Returns what IDs IDs of both the browser
70:01
Speaker A
window what is the type of this variable what is the type of this variable set of string these are all small things you need to remember okay during the interview it is very important so now we got the ideas of both the parent and child window now what is the index of the child window obviously
70:23
Speaker A
it will be one zero and one right because it will count from zero and one so from the IDS from the IDS how we can get the second one can we extract the specific value from the set collection from
70:41
Speaker A
the set I have a two values in IDs there are two values now parent window ID child window ID but I want to get the child window ID from this can we get it from the set set collection can we
70:53
Speaker A
get this directly no because we do not have a get method set is not allow the index indexing concept is not allowed so what is another way we have to convert this set into list collection array list we have to convert then we can extract by using index okay so then what I will do is I will this
71:17
Speaker A
will return the set collection that entire thing I will convert it into the list collection so here I'm directly writing so instead of taking set of a string I'm directly taking a uh list of a string okay and here so this will return the set collection that I want to convert into the
71:40
Speaker A
year list so here I'm writing new array list of driver. get window handle I combine two statements into one single statement import this are list import this list collection so is the statement is understood everyone this particular statement I combine actually what I've done so this particular
72:06
Speaker A
part will return return the set collection okay this particular driver. get window handles will return return the set collection that I'm passing into array list so once it is passed into array list that will return the list collection so once IDs are in the list format can we extract the
72:22
Speaker A
second ID yes so IDs of IDs of. get off one 0 one index will start from zero so one will be the ID of the second browser window okay so this is a ID of the second browser window or ta browser window
72:42
Speaker A
or registration page so if you know the idea of the second browser window can we switch to it yes yes now I can switch to it I can say driver dot switch to dot window and here I will pass window
73:01
Speaker A
ID I'm combining the statements so just make this like a habit instead of writing more statements I'm just combining into one single statement so whatever IDs I'm getting from that IDs I'm extracting the first ID that I'm passing into this window command switch to. window so this
73:23
Speaker A
will switch to the registration page okay so this will switch to the registration page switch to registration page so now where is our driver our driver is present in the second browser window second browser window now can we handle elements in the second browser window now that
73:47
Speaker A
is our registration page yes so once it is open registration page I'll try to enter the first name so take this field go back by.in element by X paath and do send Keys here I entering some name I just enter some name so this is element which is present in the
74:19
Speaker A
registration page okay suppose after entering the first name I want to go back to my parent page how we can go with parent page so this is our child page this is your registration page this is your registration page now I want to go to the parent page homepage how we can go back
74:46
Speaker A
again same driver. switch to. window and from the IDS we need to get zero zero Z representing parent now this will switch to Homage okay so again in the homepage I want to verify uh some l or some
75:08
Speaker A
search store I want to pass some text here on the homepage I went to homepage inspected this element then I will pass it so like this you can switch between multiple browser windows and then you can perform the option driver. find element by. XPath okay do send keys of uh
75:32
Speaker A
t-shirts okay so just by using switching this command you can switch to multiple pages and before that we need to extract those pages IDs one by one okay so this is a major option how we can open this control so now execute Java application
76:00
Speaker A
see now there are two dabs are open now can just second tab first name is entered again it went back to the previous page and then text is got entered in the search box so this is how we can open the link in the next tab by using this command so control click that will open the next
76:26
Speaker A
so everybody's understood this scenario very simple this is also keyboard action this is also related to keyboard actions so control link okay so in selenium 4 there is one more command is one more switching command is provided so for three types of switching commands we learned from the previous sessions how to
76:51
Speaker A
switch between the alerts how to switch between the frames how to switch between the browser windows so in the browser windows when you switch between the browser windows uh from selum 4 it is also provided one more switching command called switch to new window switch to. alert switch to.
77:09
Speaker A
frame switch to. window switch. new window there is another command which is provided so I'll just introduce you that method from selenium 4 onwards earlier versions it was not there so let me show you that okay okay so before that let me show you manually so when I say uh I open
77:32
Speaker A
some application on the browser let's say this is application I open in my browser So currently I launched One browser window I open this URL on the same browser window okay uh first let me write the code uh then you will see what is an output for that let me create a class tabs
77:56
Speaker A
and windows okay so I will write a small piece of code you guys can tell me what it should happen so web driver driver equal to new Chrome driver this web driver okay now I'm launching this page and immediately I launch another page so this is open card and then R hm.com so this is another
78:27
Speaker A
page so I have written driver. getet method two times first time I open this URL second time I open this URL okay now when I execute this code on the browser you can see only one URL which one
78:45
Speaker A
it is first one or second one when you open the browser first time it will open the open card.com and the same browser window opens another the URL right when I execute this this is the default behavior let's say see first open card.com on the same browser window orang hm.com is open
79:10
Speaker A
but my requirement is I first want to open this open.com like this okay and I just want to open another tab here I want to open R hm.com like this so I want to see the first page open open cut.com
79:30
Speaker A
in the second tab I want to see orange HRM dot this is my requirement but if you do like this I can see only one page at a time I cannot see both the URLs at a time right because it is open
79:44
Speaker A
only one browser window here on the same browser window I can open open card.com the same browser window I can open R CH but my requirement is I want to open this in one tab I want to open
79:57
Speaker A
the second URL in another tab how is it possible so till selenium 3 it was not possible okay but selenium 4 is provided a new command like this so what is the new command is before opening the new
80:14
Speaker A
URL as soon as you open the first URL then what you need to do is driver do switch to you need to switch your driver to the second tab switch to new window okay driver. switch on new window here
80:31
Speaker A
we have to pass window type I have to pass window type so window type is what tab okay that's it so what this command will do so as soon as you open this URL on the One browser window that keeps
80:48
Speaker A
open now this command will automatically open the next Tab and it will also switch to the next tab we don't need to switch manual again we don't need to use another switching command to switch because this command itself will open the tab and also automatically switch to the tab both
81:06
Speaker A
Works will be done in single statement this will switch to the new tab also focusing on the new tab and then I'll open this Ur after switching to the new tab I'm opening this URL just observe see the first time open card.com now second tab R HR now you can see both
81:34
Speaker A
are there again if you want to switch between these two browser windows you can switch and parall you can do the operations so this is the command is open this is available in selenium 4 onwards this is the command we introduced in selenium 4
81:49
Speaker A
onwards 4.x in all 4.x vers this command you can find so this will opens new T suppose I want to open the new browser window not on the new tab yeah now the cursor is in the second tab so as
82:09
Speaker A
soon as you executed this command opens a new tab and switch to it then open this URL now currently our driver is focused on which application orange application the second one because here we already switched to the second tab so the second application is is focused again if you want
82:26
Speaker A
to go back to the previous again you need to get the window handles and accordingly go back fine so this command is fine I open this URL in the next tab so this is a first Tab and the second tab
82:39
Speaker A
suppose I open the UR in the first browser window then I want to open the next browser window like this separate browser window not the separate tab separate browser window here I will open the new new application so now I have two browser windows this is the one this is the one not in the new
83:03
Speaker A
tab then how we can do this very simple so same command instead of a tab window type is window window type is window that's it so this will open another new browser window run as Java application see now this is one browser window this is another browser window and which one is
83:38
Speaker A
focused now driver is focused on second browser window obviously okay driver is focused on the second browser window orang CH again you can switch to it actually so both you can switch and this is the new command new switching command introduced in selenium 4 previously we have a
84:01
Speaker A
switch. alert switch. Frame switch. Window and this is a new command switch. new window and based on the parameter it will open if you say window type is tab then it will open the tab if you say window type is window then it will open the window so that is a difference understood everyone
84:32
Speaker A
yeah switching window between Windows also it is same process you'll get the window IDs and you can switch to One browser window to another browser window that is again same process okay understood everyone so this is these are all some mislan Concepts uh you can just do one round of
84:59
Speaker A
practice keyword actions and how to work with the multiple tabs and everything so I have one more concept uh small concept how to upload the file shall we proceed or we'll discuss in the next class okay so in the next class uh we will continue so just remember remember these
85:25
Speaker A
things and practice one or two rounds uh actions classes very very important guys because you will get lot of Entry questions what is an actions class what it contains you have to remember few methods from the actions class and then what is the difference between
85:40
Speaker A
actions class and action interface and build. perform and perform how we can perform Mouse operations using actions class how we can perform keyboard actions using uh actions class but some people they will use some third party AP like robot class and everything from java
85:59
Speaker A
itself like um applet and everything but they are not much comfortable they're not flexible actually because uh if you run Automation in multiple systems or multiple environments so those scripts won't work properly but if I use built-in methods which are provided by
86:18
Speaker A
selenium so most of them will work in any kind of environment okay so try to use use it and practice okay so tomorrow we will continue
Topics:SeleniumJavaSliderMouse ActionsDrag and DropWeb AutomationWebElementgetLocationX Y CoordinatesSelenium Tutorial

Frequently Asked Questions

How do you handle slider elements in Selenium when there is no clear target element?

You handle sliders by capturing their current location using the getLocation() method and then moving them by offsetting the X or Y coordinates, depending on slider orientation.

Can sendKeys be used to manipulate slider elements in Selenium?

No, sendKeys is typically used for input boxes, but sliders require mouse actions like drag and drop to move the slider handle.

What is the significance of the Point class in handling sliders?

The Point class represents the X and Y coordinates of the slider element, which helps determine its current position and how much to move it during automation.

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 →