How to Perform Mouse Hover in Selenium Webdriver

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey hi everyone this is mukesh from blonde - automation comm today in this video we are going to talk about very basic operation mouse over in selenium-webdriver for mouse over we need to use some additional classes so let's see how we can perform mouse over so the agenda for this video will be of what is mouse over what is exchange class and how to perform mouse over in selenium-webdriver so let me start with the application I created one sample application and I will give this link in the description so if you put mouse over then only this menu will come other than that this option will not be visible so if you put mouse over then only these options will come so if I click on test selenium you can see it is starting in a new tab and it is redirecting to my block this is another scenario like how to handle multiple tabs that we will that I already have created a video in the my previous playlist so you can check another scenario if I click on test Angie you will notice it is opening in the same window it's not opening in a new window so how to sister a new tab actually if you do this in selenium you will notice it will open in a new window so how to sister a new tab or new window you need to use window handles in selenium so first we will see how to put mouse over so I will start with the basic program so let me quickly write two line of a statement and here I will say mouse over demo I will use public static void main and from the previous demo I will click lead or take or two or three line of code because I do not want to write the same code again so I will explain you what it does it will simply start to Firefox it will maximize it will open a URL so URL I will give this or yeah it will give this for you worry now to work with a mouse-over if you see mouse-over is little bit advanced so all the advanced activity in selenium you have to use one actions class so actions class will be responsible for all the advanced activities I will create object of actions class and you need to pass the driver here once you pass this driver then only the sections class will work so if you put mouse over it will ask you to import remember it is actions class is capital for in Java actually if you notice classes will be in always in capital later so once this object is created now you need to call their methods so the method for mouse-over is more to move to element so you can see a move to element it will simply ask you give me the web element where you want to put mouse it gives a very good description like it will move the mouse to the middle of the web element them element is scrolled into the view and its location is calculated using get bounding client rectangle so this is they have given internal guide so only thing which you need to worry is or for the info is this only if we put the mouse at the middle of the web element so it has two method you can use this as well like you need to pass the X and y coordinate as well that we will see but this is vogue this works always with for me so let us focus on this one first then we will talk about the two methods as well so I want to perform mouse over on this so let us take the X path of that particular web element which yeah this is the one how to perform mouse over in selenium so I will start my fire book I will just scroll down yeah so you can see it is giving a very weird XPath for me which is not reliable anyways because it involves some digits do that definitely will change but I can see a text here which will make my task easy and you can see the tag name is button so what I will do first of all I will search how many buttons I have in this page and luckily I have only one so I can use either this button but let's take in a standard way I will try to search for a button which contains tax equal to automation tools so if you do not know how to write expa then I have all almost already created one video how to write dynamic XPath so I will store that web element in a separate variable but you can directly pass if you are completely fine so this is the web element where I want to put mouse over so this is the ele so I will put ele then i need to call one method called perform and you can see what is this a convenience method for performing the actions without calling the build first so we'll talk about the build as well as of now I can give you one line statement like if you have single statement like move to element only you can use perform but if you have series of operations then you can use dot del dot perform but in our operation we are not interacting with multiple operations so dot perform will work and until you do not remove the mouse from that location it will keep pointing on that particular web element so it will start Firefox and you can see it is putting mouse over on that right so this is how you can put mouse over and until you do not perform any other operation will keep mouse on that location only so now the next task is I need to click on selenium or any other so how can we do this so if you put mouse over even notice we are getting three items right so let us click on the first one so you can see I have three anchor tags here first anchor tag is targeting to my blog URL second is again pointing to my blog but it is referring to test ng and third one is for APM so what we will do first we will use the same XPath which we use now so my battery is low let me connect the charger so after this let us say I need to find out the links so as we have seen all are coming the anchor tag so again I will safe drive dot find element by tag name so if I give tag name is a it will get all the tags anchor tags from this page you can see have so many anchor text it will written around 2030 links so we need to make our XPath in this way like it should only capture these three so let us take this XPath so we are getting this after this we have three anchor link right after that I will search for a div which we do not have so let me search anchor link which also we do not have just after this so let's use this other approach we found it is coming inside a div then inside D we have this right so let's search for this particular class after mouse over so let us search doubles less div where we have class equal to okay so we got this div then after this day we have three anchor link yes we have three right nodes not visible in the screen because it only comes when you put mouse or but in the page your source you can see we are getting all this so and if you want to put any space fake then you can give the text of that particular URL so you can see if I want to click on selenium only I can provide this XPath I came if you are new to this expert then I will recommend you to watch my previous video how to write dynamic expanse in selenium so I will use two XPath first XPath will return me all the links because I will not be using any specific link then second XPath will only return me the selenium link or I will directly click on the second so I will click on the first link so let us go ahead one by one after this mouse over what I will do I will say X power and I will use this XPath since you can see this XPath will return me multiple web elements I will be using one element methods called find elements this will return me list of elements so I will say links so we need to import the list from here list you have to input from Java dot util and once you will import this list import once again yeah so let us say I want to check how many links I have so I can use like total count since links is an equal ynx is the object so I can call this size method this method is coming from collection now I can run a for loop will which will start with 0 then it will go till the number of links we have total count and I plus place so if you want to click then you can directly click but if you want any other attributes you can use that as well so here we are simply getting the number of count and if you want let us run a for loop which will get the all the hyperlinks name so I will use again one web element since this links is an object which contains list of web elements right so every time it will return me one unique web element so what I will do I will use this links object dot get method I will use here I will pass the index and it fall so once I will get this web element what operation I want to perform right now I want to check whether that link description or you can say the xref or only the text so if I want the text what I will do I will say element dot get attribute what it does it will return you the attribute which you will specify so get attribute will get the value from the given attribute of the element so we have an attribute which is called innerhtml will try if that works otherwise we have some other way as well what it will return you it will return you the innerhtml in a form of string that I will use so I will say this out and I will say links name is next so not become lengthy code so let me explain it once again so initial three lines will start your browser it will maximize it will start our web application then we are trying to put mouse over on this automation tools so I have identified using find element then we created actions class object for mouse or we call one method called move to element and it will perform move to element after move to element we have written one a small X path which will simply get the anchor links after mouse over then we are checking how many anchor links we have using the size method that we are running a for loop this follower will start with 0 and it will keep iterating the value from that list which is nothing but links object every time it will capture the inner HTML inner HTML is nothing but this selenium test ng APM and it will print in the next program or maybe just after this example I will show you how you can keep one if-else condition like put mouse over and if that particular drop-down contains a p.m. then click on it so it performed mouse-over and now let's go back to eclipse you can see it is printing the links name is selenium APM in testan g now the very interesting part of after this printing I want to write one condition if any of the link is equal to a p.m. then what then click on that l link so I will say dot click and after click I want to break the statement as well so that it will not move forward let us say right now we have only thrilling so it will work immediately but let's in consider your having a list of 15 or 20 or let's say 50 values then it will keep it reading even the condition is true so here I will say if link contains a p.m. then click on it and break the for loop as simple as that so I hope you understand this code till now we have already discussed here I am checking if X equal to e p.m. then click on this web element let us run this code and see so this mouse-over is very basic operation nowadays in every web application so if you take any e-commerce application is Snapdeal Flipkart Amazon you will find this mouse or operation so if you want to move ahead to the next step then you can use any live ecommerce web application so it will put mouse over and you can see it clicking on a p.m. and now it's never getting to my blog where you'll find the APM tutorials so you can see it is very easy now one last method which is really very interesting so apart from this link text what I will do I will print the status of each link or if you want we can print the URL s well so I will say status for every web le link I will say is enabled so you can see sometimes it happens like it is showing as a link but that link is not enabled so sometime if you need to weld it this kind of stuff then you can use this method called is enable and if you see the description it says if the element is the element disabled or not or enabled or not so it will generally return you in a form of true and false so you can see it shows in a boolean format so for every link what we will do will attach one statement I will say and the link status is status so every time it will get the status of every link and it will print so let us go ahead and run this program so you can see our using find elements we can interact with multiple web elements which shares the same locator so I have tried in Firefox in this example you can use any other browser it works same and it clicked and if you go here you can see the status is true true true for every link because all links are enabled so yes we have covered what we planned so thank you so much we'll see you in the next video if you have any doubt you can comment below in the next videos we'll discuss the different methods of actions class thank you so much have a nice day
Info
Channel: Mukesh otwani
Views: 96,959
Rating: undefined out of 5
Keywords: Selenium Webdriver, Automation, Mouse hover, Actions Class, How to Perform Mouse Hover in Selenium Webdriver
Id: mpp0zCX0R34
Channel Id: undefined
Length: 19min 57sec (1197 seconds)
Published: Wed Aug 10 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.