Header Ads

  • Breaking News

    How to perform mouse hover in Selenium Webdriver

    Many websites, specially uses jquery/AJAX have mouse hover effect in their menus. So whenever you hover the mouse on the element,  the menu will expand and you will see submenu options. For Example in hotstar.com when you perform mouse hover, the menu will expand. So for our automation testing  how to perform mouse hover in selenium webdriver ?

    How to perform mouse hover in Selenium Webdriver
    How to perform mouse hover in Selenium Webdriver

    (if you are writing first script read this => 

    How to start automation with selenium webdriver)

    In selenium we can automate mouse hover using Action class. So let's write a JAVA script to perform mouse hover in selenium webdriver. 

    how to perform mouse hover in selenium webdriver ?

    package com.automation.selenium.chrome;

    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 MousehoverInSeleniumwebdriver {

    public static void main(String args[]) throws Exception {

    WebDriver driver = new FirefoxDriver();



    WebElement menu = driver.findElement(By.xpath("//li[@class='ng-scope menu-item-li']//a[@href='/tv']"));

    Actions actions = new Actions(driver);




    This script will open the firefox and hit the url of hotstar. After opening the website it will mouse hover on the link tv and perform the action. 

    So here we are using "moveToElement" method of Actions class. This method will put mouse cursor  in the middle of the element.  Untill you don't move cursor to any other webelement it will keep on that element only. 

    Now you can access all the sub-menus which will be displayed after mouse hover. You can click on those element by finding xpath of all sub menu links and click them. 

    Please follow Software testing tips and tricks on Social media
    twitter ,

    No comments

    Post Top Ad

    Post Bottom Ad