Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Mouse Hover Action using selenium WebDriver

4.33/5 (3 votes)
23 Dec 2014CPOL1 min read 44.9K  
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)