Click here to Skip to main content
15,896,410 members
Articles / Programming Languages / Java

Mouse Hover Action using selenium WebDriver

Rate me:
Please Sign up or sign in to vote.
4.33/5 (3 votes)
23 Dec 2014CPOL1 min read 44.4K   3   3
Mouse Hover Action using selenium WebDriver

Mouse events like hovering, clicking on any element of the web page or the main menu and simulating the mouse actions/movements is not that tough in webDriver. Most of the actions can be performed directly in the webdriver. Here, I am going to discuss about a few of them. We use user interaction API constructor Actions with the moveToElement method to perform the task of monitoring the movements performed by the mouse events.

In mouser action, we use Actions(driver), object.moveToElement(), build(). and perform().

Action class is used to perform keyboard operation and mouse hover operations.

The build() method is used to compile all the listed actions into a single step.

Below is a simple script of Mouse Hover Action. First of all, we will write the scenario of what we are going to do. Here, we will automate the http://www.myntra.com and will perform the mouse hover actions.

  1. Open a Firefox browser.
  2. Navigate to the URL.
  3. Maximize the window.
  4. Now hover on the Men’s tab present in top navigational bar.
  5. Select Bags and Bag-packs from Accessories.
  6. Select the 1st item and add it to the shopping cart.
  7. From shopping cart, remove the product which has been added.
  8. Close the browser.
Java
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class mouse_hover { 

    // TODO Auto-generated method stub  
     public static void main(String[] args)throws Exception{
  
     // TODO Auto-generated method stub
     // Initialize WebDriver
     WebDriver driver = new FirefoxDriver();
  
     // Wait For Page To Load
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  
     // Go to URL
     driver.get("http://www.myntra.com/");
  
     // Maximize Window
     driver.manage().window().maximize();
  
     // Mouse Over On " Men link " 
     Actions a1 = new Actions(driver);
     a1.moveToElement(driver.findElement
     (By.xpath("//a[@href='/shop/men?src=tn&nav_id=5']"))).build().perform();
     Thread.sleep(3000L);
  
     // Wait For Page To Load
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  
     // Click on " bags & backpacks " link
     driver.findElement
     (By.xpath("//a[@href='/men-bags-backpacks?src=tn&nav_id=25']")).click();
  
     // click on the categories - Bagpacks
     //  driver.findElement
     (By.xpath("//*[text()='Categories']//following::li[1]/label']")).click();
     // Hover on the 1st bag 
     Actions a2 = new Actions(driver);
     a2.moveToElement(driver.findElement
     (By.xpath("//ul[@class='results small']/li[1]"))).build().perform();
  
     //Click on the buy icon of the 1st bag
     driver.findElement(By.xpath
     (" //ul[@class='results small']/li[1]/div[1]//div[2]")).click();
  
     // Wait For Page To Load
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
  
     // Hover over the shopping bag icon present on the top navigational bar   
     Actions a3 = new Actions(driver);
     a3.moveToElement(driver.findElement
     (By.xpath("//a[@href='/checkout/cart']"))).build().perform();
  
     // click on the remove icon
     driver.findElement(By.xpath("//a[@data-hint='Remove item']")).click();
  
     //closing current driver window 
     driver.close();   
    }
}

Hope this helps beginners.

Thanks for reading.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Tester / Quality Assurance
India India
I am Sipra Ray, ISTQB certified - Quality Analyst at Mindfire Solutions.
An extrovert by nature ,I like to interact with people and share with all what I learn.
I would love to share and answer queries and gain knowledge from forums like Code Project
Follow up with my blogs at:- siprabugtracker.wordpress.com

Comments and Discussions

 
QuestionCreate Mouse Hover Action Pin
Member 1337605923-Aug-17 22:30
Member 1337605923-Aug-17 22:30 
QuestionSelenium Interview Qustion Pin
Member 1193108724-Aug-15 0:58
Member 1193108724-Aug-15 0:58 
Can You Print using Java....

I got this question from 2 interviewers.

InDiA
Questionunable to click on span elements Pin
Member 1151015113-Mar-15 1:16
Member 1151015113-Mar-15 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.