Session 35 – Selenium with Java | Mouse Actions | Actio… — Transcript

Learn how to perform mouse actions like mouse over, right click, double click, and drag and drop using Selenium's Actions class in Java.

Key Takeaways

  • The Actions class in Selenium is essential for performing complex mouse and keyboard interactions.
  • Mouse over actions are used to reveal hidden elements on a webpage before interacting with them.
  • Creating an Actions object requires passing the WebDriver instance to its constructor.
  • Multiple mouse actions can be chained together before performing a final action like click.
  • XPath is commonly used to locate elements for mouse actions in Selenium automation scripts.

Summary

  • Introduction to mouse actions in Selenium WebDriver using Java.
  • Explanation of common mouse actions: mouse over, right click, double click, and drag and drop.
  • Overview of the Actions class in Selenium, a predefined class for mouse and keyboard actions.
  • Steps to create an Actions class object by passing the WebDriver instance.
  • Detailed example of performing mouse over on menu items to reveal hidden elements.
  • How to capture web elements using XPath for mouse actions.
  • Importance of making elements visible before interacting with them (e.g., mouse over to reveal submenu).
  • Demonstration of chaining multiple mouse over actions followed by a click action.
  • Mention of keyboard actions possible with the Actions class, such as shortcut keys.
  • Focus on practical implementation with sample code and explanation of methods in the Actions class.

Full Transcript — Download SRT & Markdown

00:03
Speaker A
Right, so in today's session, we'll see how we can perform mouse actions.
00:24
Speaker A
So normally, what are the actions we do through mouse? What are the different kinds of actions we can do?
00:46
Speaker A
Let's say mouse over, right click, double click, drag and drop.
01:03
Speaker A
So these all come under mouse actions, mouse-related operations we can do.
01:22
Speaker A
So how can we achieve this through automation?
01:47
Speaker A
So how can we do mouse operations through our automation script? We'll see that.
02:09
Speaker A
And to perform mouse operations, Selenium is provided a special class called Actions.
02:30
Speaker A
Actions is a predefined class which is available in Selenium WebDriver.
02:50
Speaker A
And in this Actions class, there are certain number of methods to perform mouse over, right click, double click, drag and drop.
03:09
Speaker A
So to perform all these operations, different methods are available in the Actions class.
03:26
Speaker A
Okay, so now we need to create an Actions class object. Through that object, we can access all those methods and accordingly, we can do the required operations.
03:44
Speaker A
So this is a most important class, Actions class, which is a predefined class available in Selenium WebDriver.
04:05
Speaker A
Now we'll see one by one how we can perform the mouse operations and how to access the method from the Actions class object.
04:39
Speaker A
So first thing, let us start with, let me put some operations. What are all things we can do? We can say mouse over action, and then we can do right click action, double click action, and drag and drop.
04:55
Speaker A
So these are the main four types of actions normally we do using mouse: mouse over, right click, double click, and drag.
05:13
Speaker A
For each operation, there is a dedicated method available in the Actions class.
05:33
Speaker A
So what is Actions? Actions is a predefined class provided in Selenium, and through which we can perform all the operations.
06:03
Speaker A
But what are the methods? We will discuss one by one.
06:24
Speaker A
So first thing, mouse over action.
06:41
Speaker A
So what is mouse over action? How can we perform it?
07:03
Speaker A
You can take any website, sample website.
07:19
Speaker A
I'm taking.
07:34
Speaker A
So mouse over in the sense, we can place the mouse on that particular element.
07:51
Speaker A
So for example, let's say here, this is my application. Let's say I put my mouse cursor on some element. Let's say I want to see desktops.
08:11
Speaker A
So I can just put my cursor on the desktops.
08:36
Speaker A
Now, as soon as I mouse over on that element, some more elements are displayed.
08:59
Speaker A
So PC and Mac. Similarly, I can put mouse over on another element like, see, I can put mouse over on components.
09:13
Speaker A
Now it is displaying some more elements.
09:34
Speaker A
So this is called mouse over action.
09:51
Speaker A
So we can place the mouse cursor on whichever element you want.
10:09
Speaker A
But sometimes, some options we can see only through mouse over action.
10:30
Speaker A
Suppose I want to see the desktop options. How is it possible?
10:46
Speaker A
Currently, it is not displaying on the page, but I want to see the desktop options.
11:00
Speaker A
The only thing is we need to place our mouse on that particular element. Then only these options are visible.
11:14
Speaker A
Same thing, whenever I place my cursor on the components, then only I can see these options.
11:26
Speaker A
Right, this is called mouse over action.
11:48
Speaker A
So you can put mouse cursor on whichever element you want on this web page.
12:01
Speaker A
So let's see how we can do this mouse over action.
12:26
Speaker A
The first action is mouse over action, very easy, very simple, and very interesting also.
12:40
Speaker A
Let's focus on this. Let's go to new package, day 35, create new class MouseOverAction.
12:59
Speaker A
Okay, now I created a new class, main method inside this.
13:17
Speaker A
Here, this is our WebDriver instance, import and new ChromeDriver.
13:38
Speaker A
And this is the application I'm launching and then maximizing the web page.
14:00
Speaker A
So now my requirement is I want to mouse over on the desktop and then again I want to mouse over on the Mac, and then I will perform the click action on the Mac.
14:18
Speaker A
So ultimately, I want to click on the Mac here, so I can go to the Mac page.
14:50
Speaker A
So before doing that, what we can do, we need to do mouse over action on the desktops, and after that, again one more time mouse over action on the Mac, and then here I will perform the click action.
15:05
Speaker A
Three steps: first, I will go to desktop option, that is mouse over action. Then I will go to Mac, that is another mouse over action. Then I want to perform the click action on this Mac.
15:25
Speaker A
Okay, now how many elements we need to identify here? How many elements we require? Desktop is one element, Mac is another element.
15:45
Speaker A
So two elements we want to capture first, okay? Then we can perform the mouse over action.
16:10
Speaker A
So let us capture these two elements using XPath.
16:30
Speaker A
So let me inspect this desktop element and go back to Selector Hub, and this is the XPath which is given. Let me copy this XPath.
16:51
Speaker A
So driver.findElementByXPath.
17:08
Speaker A
So this is one element I'm just capturing the element. I'm not performing any action. This is called desktops.
17:32
Speaker A
And the type of this variable is WebElement.
17:53
Speaker A
So now one element we captured. Next thing, one more element is there, so desktops and also Mac. One, this is also element we want to capture.
18:12
Speaker A
Inspect this Mac one, and you can see this is the element. It is saying Mac one.
18:38
Speaker A
And you can see it is showing this element is not interactable through Selenium as it is not visible in UI.
19:00
Speaker A
So this is the problem with this element. Actually, Selector Hub is clearly saying the element is not.
19:13
Speaker A
Suppose if you want to directly click on Mac one, it's not possible. First, we need to make that element visible.
19:27
Speaker A
How is it visible? First, we need to go to desktops mouse over action. Then it is visible.
19:46
Speaker A
So once it is visible, then we can perform any type of action.
20:08
Speaker A
So I'm getting this element, and then this element also I need.
20:27
Speaker A
So I can say driver.findElementByXPath and specify the XPath.
20:48
Speaker A
It is just like a link, actually menu options. These are all menu options.
21:05
Speaker A
If I inspect this desktops, what is the tag? It is showing just an anchor tag.
21:22
Speaker A
Okay, this is the element links. They are basically called links and present inside the menu items as part of menu item.
21:40
Speaker A
They're not button actually.
21:57
Speaker A
Okay, if it is a button, there is an input tag. If it is a link, there is an anchor tag.
22:18
Speaker A
Okay, fine.
22:38
Speaker A
So now, yeah, this is another element which is Mac that I'm going to capture. Mac, and this is also WebElement, right?
22:54
Speaker A
So now we capture two elements because we want to perform mouse over action on these two elements.
23:09
Speaker A
Fine, so elements are ready. Now we need to perform the mouse over action.
23:31
Speaker A
So mouse over action is a mouse-related action.
23:47
Speaker A
So all mouse actions we can handle only through Actions class object.
24:04
Speaker A
So how can we do the mouse over action?
24:22
Speaker A
First thing, we have to create an object for Actions class that is a predefined class which is available in Selenium.
24:35
Speaker A
So I'm creating Actions act equal to new Actions.
25:16
Speaker A
So this Actions class we have to import from org.openqa.selenium.interactions.
25:34
Speaker A
So this is a package which we have to import.
25:52
Speaker A
So the Actions class is part of interactions package.
26:16
Speaker A
Now whenever you create an object for Actions class, we have to pass driver parameter into Actions constructor.
26:43
Speaker A
So this is mandatory because Actions class contains a constructor which is expecting driver as a parameter.
27:02
Speaker A
So that we have to pass. This is one prerequisite.
27:12
Speaker A
So how to create an Actions object? Actions act is an object, a variable, you can put any type of variable equal to new Actions and then driver.
27:32
Speaker A
So we have to pass the driver. We should not forget this.
27:42
Speaker A
And now by using this Actions class object, we can access all the methods which are available in Actions.
27:49
Speaker A
For example, let's say act dot, see these are all the different methods: double click, context click, click and hold, click.
28:09
Speaker A
Okay, so drag and drop, drag and drop by. There are n number of methods in the Actions class.
28:33
Speaker A
And not only mouse actions, we can also do keyboard actions by using Actions class object.
28:55
Speaker A
We can also perform keyboard actions like sometimes we do shortcut keys, we press shortcut keys: Ctrl Z, Ctrl V, Escape, Enter.
29:17
Speaker A
So these actions also we can do through the Actions class object.
29:37
Speaker A
So the various methods are present in this context menu.
30:07
Speaker A
So we'll see one by one. Today we'll focus only...
30:27
Speaker A
clicking we got a context menu in that we will click on the copy so click on copy how to click on this let's inspect this element so right click inspect you can click on anywhere inspect and then I need to open in another browser yeah so after right clicking you're getting these options now I
30:54
Speaker A
want to click on this copy so let's inspect this only this copy element right now we can see one more relative xath is given capture the relative xath of this copy and now go here say driver. find element by. xath and we can do normal click on this so we can directly call Dot click this is a
31:20
Speaker A
normal click action and after clicking on this you will get an alert window so that I want to close close alert box how to close it first we need to switch to this alert box and then we can accept
31:36
Speaker A
it so driver. switch to do alert do accept so switch. alert we switch to the alert then accept it because that's the only action I want to perform so I can switch suppose if you want to perform any other action suppose if you want to capture uh some Tex from this alert box and
32:00
Speaker A
everything then what you can do you can store this alert window in another variable alert type of variable then use get text that will get the text value and after that you can do accept but here I just want to accept it so I'm just call driver. switch. alert doxel so this will close your alert
32:19
Speaker A
box simple so right click then I click on copy and then I got alert box then I have closed it and it will go very faster so I'll put some weight statement here thre do sleep I'll just pause our
32:34
Speaker A
code for some time so that we can see the alert box or else it will go very faster done so now execute you can see it is successfully right click copy now we got an alert and it is is
32:58
Speaker A
also closed so this is how we can simply do right click action so what is the method act class dot context click of element dot perform context click what is the method for this very very important there are just four to five methods are there you have to remember them during interview
33:22
Speaker A
it is very very important they will ask you just can you tell me some action class methods then you should able to tell okay context click off element so this is a method context click is nothing but a right click fine so is this clear to everyone how to perform the right click it's
33:43
Speaker A
very very easy straightforward driver uh context click is a method and whichever element you want to perform right click specify dot perform right now let's move on to the next one double click action this is also sometimes we required in our test cases double click
34:03
Speaker A
so when you do normal click on the button nothing will happen sometimes we do double click then only action will be performed so let me show you an example for double click action okay so let's go to another page
34:31
Speaker A
yeah so look at this page this is a page I have and uh there are two frames are there in this page so you can see field one and field two can you see this one field one and field two or let
34:44
Speaker A
me maximize it okay here it is a field one and a field two so just observe the functionality the field one there is some text which is already populating which is hello world now field two is empty nothing is there so I'm just clicking on copy text this is a button when I click on it
35:05
Speaker A
nothing is happened when you do double click the same text is got copied into the second same text is copied into second box how it is happened because of double click action so when you do double click then the text is got copied into the second box now we need to verify this
35:25
Speaker A
functionality so whether the same text is copied into the field two or not we need to check after Double clicking in action okay so let's and not only this we can remove this and we can pass whatever text you want and when it said double click the same text is got copied into
35:42
Speaker A
the second box so now I want to verify this functionality the same text is copied in the second box after Double clicking or not we need to check it so let's do this take another example new class double click
36:05
Speaker A
action okay so now we created web driver object and this is the application I launched and then maximize the browser window now so let us do step by step so first thing by default when you open the page field one is having some value by default and then field two is empty and when you do right
36:37
Speaker A
click the same text is copied into the second box now how many elements we need to interact here three elements one is the first input box okay and if you want to pass some text in this then we can inspect field one or if you want to copy the same text in the second box you don't
36:55
Speaker A
need to identify the field one but what I want to do is I want to remove this existing textt and I want to pass a new text so field one also I want to recognize and then field two also we
37:06
Speaker A
need because after double click the text is same or not we need to check so field to element also we need to identify and copy text button also we need to identify totally three elements we want to identify or let's write the locators for those three elements so you can say driver. find element
37:28
Speaker A
and by dot xath okay now I'm just going to show you one important feature from the selector Hub see here there are three elements right so if you want to get an xath from the selector Hub what you will do normally you inspect this and go to selector Hub like this and you will capture the
37:50
Speaker A
xath and use it in automation after that next time you go to next element and again copy the text path and use it in your application after that again you can go to this element then again copy the X paath so this process you repeat multiple times for multiple elements right suppose I have
38:10
Speaker A
more elements on your web let's say you have five elements or 10 elements I want to get xths of all those elements whatever elements are displayed here I want to capture all the xath at one shot I don't want to repeat the same process again and again so that we can do using selector very
38:28
Speaker A
useful feature which is available I will show you that feature now so to get all the elements locators at one shot what you can do is there is an option here so there is an option here the second third option click to generate locators page and multiple selectors the first second third
38:49
Speaker A
option in the selector Hub you can see the third option what is the third option click to generate locators locators page and multiple selectors so once you click on it you will get a empty page here okay and in this empty page what you need to do is just get this uh Arrow
39:10
Speaker A
Mark and whichever element you want to locate just show them one by one so I'm just clicking on this Arrow Mark observe very carefully I'm highlighting the first one and clicking on the first element see it is automatically captured immediately click on the second element second
39:28
Speaker A
element and one more time take this inspect second element captured one more time taking the inspect and third element captured now you can see three xarts are captured for these three elements not only exper it is also captured CSS and if ID is available ID is also captured if the name
39:47
Speaker A
is available names also captured currently names class link text are not there so they have not captured if they are available they also captured so this is another use useful feature which we have now if you want to copy these three xath at once you can use this option click to copy all
40:05
Speaker A
the xath just copy it now go back to your script and paste it over here so these are the xath we directly capture for these three elements so this is a very very useful feature which is available in the selector Hub so third option click to startop and after capturing all the element you
40:23
Speaker A
can stop it I'm again one more time I'm clicking on this it is get stopped so third option you can just explore it just click on it and uh after uh after starting this option you can inspect any
40:38
Speaker A
of these elements multiple elements on the web page one by one it is automatically create all the expart and now you can export it okay that option is there so now I'm going to use these three xarts to capture those three elements now I can say driver dot find element by. xath see how
41:02
Speaker A
it is reducing lot of effort for us so very very useful so you can say by. x paath dot so I'm not performing any action because we want to get the data and everything so I just want to get
41:15
Speaker A
this element in a variable so I just call it as a box one first input box I a box one this is a web okay and this web element I can import and same thing I can do another
41:34
Speaker A
one web element box two second input box and this is the xart for the second input box okay this is also done now the third element is a button can say driver do find element by do X path
42:01
Speaker A
and put this X paath in the double quotes this is a button element so this is also web element I can say web element but so now three elements have captured so box one box two box so this is
42:20
Speaker A
a very simple guys let me explain one more time you're not seeing properly see if you want to capture multiple web element ments can go to the third option there is a third click to generate locators page and multiple selectors it will be on if you want to clear the existing
42:37
Speaker A
one you can just click on the delete button so it is everything is clear now you can inspect the elements one by one so I'm inspecting field one captured now one more time go to inspect the second field captured now the Third Field captured everything will be captured like this one after
42:54
Speaker A
another after that you can just do copy option click to copy be all exper this is the option just do one or two rounds you will be familiar with this very simple all right now I captured the three elements box one box two button now we want to perform the double click action okay
43:13
Speaker A
so before doing double click action I want to do something on this application let me open it okay so first thing I don't want to pass the same text I want to pass a new text so before passing the new text we have to clear this the existing value we have to remove and then
43:38
Speaker A
I can pass a new text after that I can double click here and whatever is there in the first box same thing will be copied into the second box so first of all I want to clear the field
43:48
Speaker A
one so take this box one this element and how to clear the text from the input box yes Dot clear so this will clears the box one clears the box one after clearing on the same box I want to
44:09
Speaker A
pass some text by using send keys I'm passing some text called welcome okay input box is done after that in the second input box do we want to pass anything nothing we don't pass anything in the second box so after passing the text in the field one we have to just double click on
44:30
Speaker A
this copy text so we need to perform the double click action on the copy text so let's perform the double click action here so to perform the double click what we need to do double click action so double click action on the button so first we need to create an action SL
44:51
Speaker A
object actions act equal to new actions pass the driver instance and import these actions right so after that we need to call one method direct method called double click act dot double click this is the method act dot double click on which element you want to
45:15
Speaker A
perform double click this is the element that I want to pass DOT perform build. perform or perform anything so this will perform the double click action act dot double click of element on which element you want to perform double click that we need to pass DOT perform so this will
45:36
Speaker A
perform the double click action so let us cross check and we do validation also Java application so observe and it is launching my application it is little bit slow we need to wait for some sometime okay now it is launched maximized now it should clear the first field and then perform the
46:23
Speaker A
double click action so it is not doing what what is the problem yeah we got an error see the error first what it is saying no such element exception unable to locate an element when you will get this exception the in the xath is perfectly fine the xath is correct and everything is
46:41
Speaker A
fine but it is unable to locate an element no such element exception is unable to locate an element because that element is inside the frame that element is inside the frame so that's the reason we are not able to locate this element when inspecting this element go to selector
47:01
Speaker A
Hub see what it is saying when I inspecting this element selector Hub is giving one alert this element is inside the same origin if frame switch inside the if frame to access it through automation also it is giving the xath of the I frame frame XO also it is giving so first we
47:26
Speaker A
need to switch to the frame after switching to the frame then we can interact with the element okay after that we can interact with the frame and one more thing if you look at the if frame here there
47:40
Speaker A
is ID attribute and name attribute is also there ID attribute is what iframe result and here also name is also if frame result so if ID name is directly available you can switch to the frame directly so what I will do is before interacting before getting these elements I want to switch
48:00
Speaker A
to the frame so because all these elements are present inside the frame so switch to frame how to switch it driver dot switch to do frame and if ID or name is present you can directly specify them in the double quotes So currently this iframe ID is there iframe result or name whatever both are
48:23
Speaker A
same that you can directly specify okay or else you can pass the entire frame as a web element here or you can directly specify name or ID directly so this command will switch to the frame and then it will get the elements and then it will perform the action let's execute now one more
48:44
Speaker A
time so just observe so hello world will be cleared and then it will enter the welcome then same thing is copied into the will be copied into the field two yes now we can see the same text is got copied into the second field because
49:20
Speaker A
it is successfully done double click action okay now I want to verify the same text is got copied in the second box or not I want to verify the same text is got copied into second box or not I want to verify that okay now how we can
49:39
Speaker A
verify how to put the validation point so whatever text we passed the same text should be displayed in the second field so we already passed the value here the same text should be there in the second box how we can
50:01
Speaker A
validate what is the validation box two should contains welcome this is the same text we passed so that should have so if you want to verify box two contains the same text or not how we can do how we can capture the text how we can capture the
50:32
Speaker A
text here so we already captured the element right so can we use get text from the box two so box two the value is already present it is already copied now we need to capture the text right now we can
50:49
Speaker A
say can we say box two. get text yes or no box to. get text so that will give the text string value right or else what you can do you can put directly in if condition if box two. get
51:04
Speaker A
text okay do equals do equals you can directly specify the value which we have given this is a if condition and then you can put here system do println here successfully copied text copied else if both are not equal then you can say text not copied properly okay this is a validation suppose
51:42
Speaker A
if you want to store this get text value in some other variable you can put this another variable no problem you can make it another statement box to. get text and here text and this is a string variable okay this is a string variable and now we can compare the text with our
52:04
Speaker A
equals method so instead of this I can just remove the text right this is the way we can write fine so now box 2. get text method will capture the text value and store it into the variable now we are comparing that value equals to welcome or not right this is a validation
52:27
Speaker A
so if you want to compare with the box one value you can still compare no problem but you need to capture the text from box one also okay box one.get text another statement box to.
52:36
Speaker A
get text store both of them in two different variables and you can compare that is also other way we can do it okay because we ourself we passed the value so I'm just comparing with that okay otherwise you can capture after passing this value you can capture the value from the box one
52:54
Speaker A
you can capture the value from the box two then you can compare it any anything is correct okay all right now execute and see what will happen so remember to capture the value from the input box we use get text method and earlier also we used get text method to capture the text from the web
53:14
Speaker A
element right now observe this scenario when I run as a Java application maximizing the page so this page takes too long time we need to wait for a few seconds okay now whatever text we passed in the field one same thing is got copied both
53:55
Speaker A
are exactly the same now if I go back and see the output text not copied properly text not copied properly why it is given some validation is got failed why it is failed whatever text we captured that is not actually matching with welcome why it is matching why it is not matching because we
54:18
Speaker A
are giving any uppercase characters here also we have given uppercase character exactly the same right equals method should match it but somehow it is not matching so how we can verify first of all let us see this get text method is able to capture the text correctly or not okay so for
54:35
Speaker A
that what I will do is I will try to print text value here okay we'll see captured value is I'm printing the text this is how we can cross check even even if you give equal ignore case it will
54:53
Speaker A
not work because exactly it is giving same value we are giving welcome and welcome but still not matching so that's not a problem comparison is not a problem the problem is what whatever value are getting into the text that is not matching with this one but this value are given correctly but
55:11
Speaker A
we need to doubt we are doubting about this value text value so for that reason before comparing I'm just printing the value okay before comparing I'm just printing the value so if you print the value we will know that right what is the value it is rning so let us execute and see one more
55:27
Speaker A
time whether the get text method is correctly giving the value from the second box or not that we want to check first so comparison there is no problem with the comparison we are comparing both words in the uppercase characters using equals
55:46
Speaker A
method and also text is also successfully copying so whatever we passed in the field one same thing is also copied into the second box this is also perfectly fine and comparison also perfectly fine but the problem is here is captured value is not getting anything captured value is empty capture
56:13
Speaker A
value is empty that means get text method is not able to capture the value from the box two getex method is not able to capture the value from the box two that is a problem here so now we need to
56:28
Speaker A
understand something very very important even in interview also which is very very important get text method cannot get the value in some times what is the scenarios I will tell you and also we'll see how to capture the text from it without using get text if I look at this page just look at
56:47
Speaker A
this page and see the HTML of this I'm inspecting this element field two and this is input box do we have any text displayed in the field two here just like a field one in the field one there is
57:02
Speaker A
a value attribute and there is hello world but in the field two are we getting anything here nothing so get text method is always capture inner text of the element remember this point very very important get text method will capture only inner text of the element what is inner text do we have
57:24
Speaker A
inner text for this input box one or input box do we have inner text do we have inner text no what is an inner text this is the inner text you can say copy text is there this is called inner text
57:39
Speaker A
which is included in the greater than and less than that is called inner text so do we have inner text for the field two and field two field one and two no we do not have inner text so whatever
57:49
Speaker A
text is displayed here they are not inner text if they are inner text then only we can capture the text by using get text method so let me give some examples here so that you can easily understand so here okay get text is not working then how we can actual how to get the actual
58:11
Speaker A
value get text is not working because they are not inner text but they are the values of value attribute if I look at this hello world what is this actually the value of value attribute right this is what what is this this is a value of value attribute and after Double clicking what is
58:37
Speaker A
happening the same value is copied into the second box now in the field two whatever the value which is copied from field one to field two now field two value is what same again so field one field two is having value attribute okay when you do double click what happens whatever the values
58:59
Speaker A
there in the field one the same thing is copied into field two now what is a value of the value attribute of field two same value right so if you look at the implementation code this is the HTML
59:13
Speaker A
code they have implemented JavaScript code they have implemented to design this just observe this is the HTML of the field one and there are some attributes type ID and value and what whatever the by default whatever the text is displayed here that is assigned to the value attribute there
59:32
Speaker A
is no inner text now after Double clicking what happens so this is the button on double clicking what happens they are doing something here so this method will be called what is this method is doing here field two the value will be set field two value attribute will be set with what from the
59:55
Speaker A
field one get element by ID you can see from the field one this is the JavaScript but it is very easy to understand from the field one capturing the value assign the field to Value basically what it is doing when you do double click action it is capturing the value from the field one
60:16
Speaker A
and assign the same value to the field two value attribute it will do runtime at run time when you do double click action this method will be called this function JavaScript function will be called and action will be performed so what we need to understand here there is no inner text for these
60:34
Speaker A
two elements only we have a value attribute so this text is belongs to what value attribute okay remember now by using get text we cannot get that value because there is no inner text but what is the other way we can capture the value attribute we need to capture the value of value attribute
60:56
Speaker A
that is our ultimate goal now instead of get text instead of get text what we need to do we have to call one more method called get attribute of which attribute value we want to capture which attribute value we want to capture value attribute that we have to specify in the double quotes value so what
61:23
Speaker A
this method will do is get attribute method will get the value of the attribute which we specified here so currently we specified the value attribute so this will return the value of value attribute so this will exactly give you the value so let's execute and see I will give you more examples
61:45
Speaker A
just hold on so get attribute of value that's a method we we have used alternatively instead of using get text method now observe this whether we are able to get the value or not and which is very important what is the difference between get text and get attribute value when to use get text when
62:08
Speaker A
to use get attribute value I will show you more examples let's finish this one it is not display in the Dom why because the copying will happen only at the run time not at the design time so Dom will give you whatever things will happen only in the design time not in the run time so that is
62:28
Speaker A
the reason when you inspect this field to element after copying you cannot see the value attribute because this will happen only runtime so the JavaScript function will be called only when you click on the copy button so that's the reason it will not show you in the Dom structure in HTML
62:47
Speaker A
okay now you can just go back and see yes you got a captured value is welcome now comparison is passed now the text is going got copied this is the way we have to capture the attribute now let us try to understand this with more examples so that you will be more clear because this is
63:07
Speaker A
very very important lot of people having more confusion on this when to use get text when to use get attribute of value okay so I'll give you some examples you should tell me that okay what is the difference between get text versus get attribute of value so whichever attribute value
63:29
Speaker A
you want to get that attribute you can specify so in this bracket you need to specify attribute okay now let me show you some example you guys can tell me suppose I have HTML like this input ID equal to XY Z ID equal to XY Z input uh ID equal to XY Z and in this I have something called some
63:56
Speaker A
value which is inner Text slash input now if you look at this HTML do we have inner text in this inner text yes welcome is inner text so ID is also available ID is what id is what XY J ID is
64:11
Speaker A
what attribute okay inner text is welcome now my requirement is I want to capture this inner text of this web element how to capture this inner text Dot get text do get text so element do get text what this will do get text method we return the inner text we'll Returns the inner text so
64:41
Speaker A
what is the inner text here welcome is a inner text now suppose you have WR get attribute of ID because here we don't have a value attribute and I have ID attribute I'm say ID so get attribute of ID what this will return it will return the value of ID attribute so this will Returns the
65:11
Speaker A
value of attribute it can be any attribute so the output is what XY CH so now you understood with this example now let me give you another one let us say I have same input value equal to some
65:31
Speaker A
XY J value equal to let's say welcome and there is no inner text for this now I want to capture the value what is the method we can use I want to capture this get attribut right because we don't
65:51
Speaker A
have inner text here this is got closed we don't have a inner text so only get attribute method we can use get attribute of value that we need to put in the double quotes so this will returns welcome okay so now you understood the difference if anybody ask you
66:13
Speaker A
during the interview you should able to tell get text method always returns the inner text of the web element it can be any type of emment inner text if it is available it will return if inner text is not available it will return nothing
66:27
Speaker A
get attribute is a method which will return the value of the attribute of the element it can be any attribute ID attribute name attribute class or whatever it is but their attribute should be available in the HTML so get attribute is a method which will return the values of a attribute so get
66:46
Speaker A
text method will Returns the inner text of the element whereas get attribute will Returns the value of the attribute so this is a conclusion I hope everybody is clear so now you know when to use get text when to use get attribute of value so now we successfully perform the double click
67:10
Speaker A
action so what we have done we pass some text in the first input box and then we perform double click action and from the second box we capture the value and then compared it okay so where we have done the Double C action this this is how we can do the double click direct method double click
67:29
Speaker A
and specify the element so to perform the double click action what is the method double click of element okay three methods we discussed for Mouse over Mo to element for right click context click double click double click direct now we'll move to the next one apart from the value tag do
67:56
Speaker A
we have any other tags that can hold input text like this example most of the times you can see sometimes the text attribute also not only value sometimes you can see text attribute so instead of value we can see sometimes text attribute okay in that case also you can capture the
68:16
Speaker A
text yes now let us move on to the next one so I hope everybody is clear how to perform Mouse over right click and then double click finally very interesting stuff drag and drop okay most important scenario sometimes in our applications we need to do drag and drop
68:41
Speaker A
so drag and drop in the sense only within application not from your desktop to your application sometimes you you will drag your files let's say you want to upload some files in the Google Drive you will open the Google Drive and whatever files in your desktop you
68:54
Speaker A
can just drag and drop into Google Drive that won't happen files from your desktop cannot be uploaded into the Google Drive that is having a different mechanism but that is also drag and drop but that is a different that is uploading the files but here this drag and drop is should
69:10
Speaker A
happen within your application so there are two elements are there in your application one element you can drag into another place one place to another place you can drag within your application so we'll see how we can perform the drag and drop so the next example is drag and
69:26
Speaker A
R so let me show an application yes now we can see this page Dragon drops demo so here I have different Capitals in one list another side different countries boxes are there so in this we can do Dragon drop for example let's say I want to move this Rome to Italy so I can just do dragon drop
70:02
Speaker A
like this I'm doing manually now even if you do incorrectly no problem but ultimate goal is we need to drag and drop so similarly Washington I can just move into the United States and same thing you can just move into somewhere even if you do incorrect no problem you
70:22
Speaker A
can do right click and drag and drop like this everything is got moved so how we can perform this drag and drop very easy listen carefully so let's go back create another example new class drag and drop drag and drop action
71:01
Speaker A
okay so let's import web driver and driver. getet application then driver. manage window ma right so now let's see how we can perform the dragon it's very simple and before performing Mouse action we need to create a actions class object that we will know so actions act equal to new
71:23
Speaker A
actions and we need to pass the driver instance and after that we need to import actions okay so now we'll see the dragon drop action so first of all whenever you perform Dragon drop only two things you have to know one is Source element Target element from where to where you need to
71:44
Speaker A
move that we need to capture so here the Rome is one element that I want to move what is the target Italy is a target so Source element is Rome Target Target element is what Italy so we need to First capture these two elements before performing drag and draw so let me inspect this
72:03
Speaker A
roome element so wherever it is in the Box no problem you can capture go to selector Hub and it is giving some relative locator so ID it is taken ID is available ID equal to box you can take xath or ID whatever so I'm getting the source element driver. find element
72:26
Speaker A
by do xar and I'm not doing any action on this I'm just getting that element this is a Rome and type of the variable is what web element import this this is our source element now we need to capture
72:42
Speaker A
the target element so Target element is what Italy inspect and this is our Italy Target element so I can say driver dot find element by. xath and this is our Target element I can say this is also web
73:02
Speaker A
element Italy okay now I know Source element I have a Target element I think xath is not correct perfect okay so now this is our source element ital is a target element now we capture then we need to perform from Dragon so take the object act dot
73:29
Speaker A
drag and drop direct method drag and a drop and here we have to pass two parameters Source element and Target element so R is a source element comma Target is a it element so two parameters we need to pass from where to where we want and after completion dot
73:49
Speaker A
perform just call perform that's it one single statement will perform the drag drag it okay so this is our Dragon so now let us execute and see Rome to Italy yes see automatically it is dropped similar way we can also drag other options so for example
74:26
Speaker A
I want to drag some other City let's say I want to drag this Washington into United States now what is a source element Washington is our source element take this box three so this is Dragon drop one another
74:47
Speaker A
one drag and drop to driver. find element by. X pass so this is a another element this is a web element Washington and what is the target element is United States so inspect this target element get this x
75:17
Speaker A
path i. find element by. X path dot this is a US web element right now we have a source element and Target element again same process same act object dot drag and drop Washington and us do perform that's it so like this you can repeat the same thing for other elements run as Java application
75:59
Speaker A
right so Rome to Italy Washington to United States perfect so this is how we can simply do dragon drop so direct method Dragon drop Source element Target element so what is a method drag and drop of source element comma
76:25
Speaker A
Target so this is how we can perform the dragon so very easy very simple so Mouse over move element right click is for context click for double click direct method available direct double click for drag and drop direct method Dragon so these are the four methods and there are
76:45
Speaker A
some more one or two methods are there how to handle the slider and everything that I will show you in the next session so horizontal slider vertical slider how we can handle the same thing so same process for Washington if your application is allowed you can get back
77:00
Speaker A
otherwise you cannot go back okay for example uh I just moved in Italy I already moved so if you want to get back yeah you can get back same process here it is you need to identify this Source element Rome and you need to identify this entire box because this is the source
77:19
Speaker A
now so now you can go back like this I'll give you some more assignments on this you can try yeah you need to switch to those frame so if two elements are present in the two different frames okay so you're identifying The Source element from
77:36
Speaker A
the frame one target element from the frame two then you need to drag and draw so you can do that in that case also we can still handle if two elements are in two different frames but switch to frames is required before locating element and before for dragging that
77:56
Speaker A
element okay so now this is how we can perform the drag so now we let us understand something about in the beginning I told you build and perform right so build. perform and perform so build means what it will create an action perform will complete an action so when do we
78:20
Speaker A
use build method when do we use perform method so if I use build. perform and if I use perform method both are same no difference you can use build out perform perform okay but sometimes we have to create an action and after sometime we can perform the action initially I will create
78:43
Speaker A
an action that means I will build that action later on I will perform the action in that case we use build and also whatever action you created you can store that action in another way varable somewhere other variable that you can perform later so I'll show you an example so how we can
79:03
Speaker A
do that and what is the scenario where we can store the action in a variable let me create an action and here in the selenium itself there is an interface called action interface actions is a class action is an interface I'll tell you what is the difference that the main intention of showing
79:23
Speaker A
this example action versus action actions versus action in the same example I'll show you the build where we have to use a build action okay so action versus action so you can take any example so I'll take a right click example and this script we' already done previously so import this web driver
79:58
Speaker A
okay so let me remove this part okay so if you look at this uh I launched this page and this is basically right click action so on this element so open this page and here you can see this button when you do right click right click
80:20
Speaker A
action will be performed right so how we can do this right click action normally we can capture the element we take the access class object and if you want to perform the right click what we have done act dot context click is a method which we have used and you pass the element dot perform
80:38
Speaker A
this is the way we have done or else another way what we have done is we can use builder. perform Ina perform you can also use build dot perform so as I said both are same so first it is creating an
80:53
Speaker A
action then performing it and here it is directly performing the action so now let us take the first one so in this particular thing first it is creating an action immediately it will perform the action but I don't want to perform this action immediately I want to build that action but I want
81:12
Speaker A
to perform this action later then what I can do is I just call only build method this will create an action okay but how we can perform this action later we want to store this in a variable so whatever action is built which we have to store in a variable and if you have that variable then
81:35
Speaker A
you can perform it whenever you want in future okay otherwise it is not possible we have to store this action or whatever action is built which we have to store in a variable and that we can perform later so what is this build method is rning is action interface AC ction type of
81:55
Speaker A
variable it will return return because this is the action actually it is building an action so I'm creating a variable called my action and the type of this variable is what action type of variable and this action is an interface which we have to import from org open. cinium do interactions okay
82:18
Speaker A
so what is an action variable will do this can hold the action which is already built so the build method is build that action that means it is created an action that we are storing into a variable and the type of this variable is what action type variable so later I want to
82:38
Speaker A
perform this action then what I can do now can take this variable action variable dot perform this is how we can do so in the first statement what we are doing we are creating an action and storing into a variable in the second statement what we are doing we are performing we are
83:04
Speaker A
performing or completing action here we are creating building or creating building or creating an action and storing into a variable in the second statement we are performing or completing an action so this is how we have to use actions class variable so whenever you
83:31
Speaker A
say only build this will just build that action and where you want to store it in a variable is required and that variable should be action type of variable now what is the difference between action versus actions action versus actions very very popular inry questions very very popular in
83:55
Speaker A
question what is the difference between action and actions or actions versus action these are the difference actions is basically a class it is a class will be used to perform Mouse actions all Mouse operations methods are available in the actions class we can create an object for
84:18
Speaker A
that through the object we can access all the methods action is an interface we cannot create an object but we can create a variable so it is an interface will be used to store created actions which we can store created actions so everybody's understood what is the difference
84:38
Speaker A
between actions and action and when to use action variable whenever you want to store a created action or build an action in a variable that variable should be action type of variable and that variable you can use to perform the action later on so here we are creating an action just
84:58
Speaker A
we creating an action here we are performing an action clear everyone the concept is clear please confirm in the chat box what is the difference between actions and action when to use action interface very popular interation from the actions
85:21
Speaker A
class so so how to handle Mouse operations by using actions class methods what are the actions class methods are available you have to remember them for right click double click Mouse over drag and drop so you have to remember these methods and what is the difference between
85:41
Speaker A
build. perform and perform so build. perform is creating an action immediately performing it or completing it only perform is the perform is also internally can build that action immediately perform it there is no much difference actions and action actions is a class which contains all
86:01
Speaker A
methods through methods we can perform the mouse operations we can create an object because it is a class there action is an interface we can create a variable but we cannot create an object we can store actions into action variable so this is a concept of actions and action I hope now you
86:22
Speaker A
understood this many people are having some confusion here so I just clarified this fine so just practice these things for today's session and by using these action class methods we can do some more actions we can also handle the slide bars okay sometimes you can see price element
86:41
Speaker A
right we can just drag and drop there's also kind of a drag and drop but we need to handle the different methods and horizontal scrolling vertical scrolling and uh we can also perform the keyboard actions sometimes when you do shortcut keys we click on key keyboard right that also we
86:55
Speaker A
can handle using Mouse actions there are certain methods are there to handle the keyboard actions so that we will discuss in the tomorrow session right so come to the assignment Parts based on today's session I'll give you few assignments you guys can try the first assignment double click so
87:19
Speaker A
just go to this place double click and drag and drop both are there in this page let me open this okay so if you look at here uh this is the double click same thing actually whatever we have done
87:33
Speaker A
in the session same thing same some text you can fill here and when you do double click same thing is copied you can just check it and drag and drop is here you can see this is small box this
87:45
Speaker A
is another box so this small box we need to drag and drop like this okay now we can see dropped if you want you can verify this message also you can capture this dropped after drag and drop you can verify this so toggle is a different thing okay toggle we can capture
88:05
Speaker A
it by using get text method so now we can capture this tool tips toggles we can handle the different so this is a drag and drop and this I'll show you tomorrow session slider how we can handle this okay and this is one assignment next one drag and drop
88:26
Speaker A
so go to this yeah you can see this uh there is a balance sheet here you can see debit side and credit side account amount account amount here there are some values what you need to do is you need to drag and
88:47
Speaker A
drop for example it's say Bank can just drag and drop like bank and 5,000 okay and sales let's say 5,000 sales is not coming here sales come here sales and 5,000 you need to drag and drop all these above items including numbers and text Bank sales owners inquiry loan everything you need to
89:11
Speaker A
drag and drop in this balance sheet but how you can drag and drop I will give you a small document for reference and by using by seeing the document you can perform the drag and draw I will share the
89:23
Speaker A
solution also so for this we can drag so initially the box is like this and after performing drag and drop it should become like this so on the debit side you can see bank and 5,000 in the credit
89:39
Speaker A
side you can see sales and 5,000 you don't need to move all items just move one or two items from the debit side bank and 5,000 should drag so here 5,000 and Bank should be in the debit side and
89:53
Speaker A
sales and 5,000 sales and 5,000 should be in the credit side so try to do this assignment it's very useful and you will learn a lot of things I will provide the solution for this but try yourself first and then refer the solution and this is another assignment based upon today's
90:14
Speaker A
session okay so that's all for today's session I will stop here we'll continue tomorrow session
Topics:SeleniumJavamouse actionsActions classmouse overright clickdouble clickdrag and dropWebDriverautomation testing

Frequently Asked Questions

What is the Actions class in Selenium?

The Actions class is a predefined class in Selenium WebDriver used to perform complex mouse and keyboard actions like mouse over, right click, double click, and drag and drop.

How do you perform a mouse over action using Selenium?

To perform a mouse over action, you first locate the target element, create an Actions class object by passing the WebDriver instance, then use the moveToElement() method followed by perform() to execute the action.

Why can't Selenium interact with some elements directly?

Some elements are not interactable because they are hidden or not visible until a mouse over action is performed on a parent element, making them visible and interactable.

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 →