Site Loader
Auckland, New Zealand
In this post we will discuss one of the common yet easiest feature called mouse hovering in Selenium. There are many situations where we might need to hover a to control in applications UI and the UI will bring the interactive options or menu or window on-the-fly (using JQuery or Javascript). Then we may need to click on the control and perform some other operation used for automation testing. For doing these operations, Selenium has a special package called org.openqa.selenium.interactions which has a class called Actions, which has lot of special classes to perform operation such as hover as discussed in the last post of drag and drop. We can perform the mouse hover and click in our demo site as shown below Here is the complete video discussed about the Hover and hover click operation, Here is the code snippet Hover
public static void Hover(WebDriver driver, WebElement element) {
	Actions action = new Actions(driver);
Hover and Click
public static void HoverAndClick(WebDriver driver,WebElement elementToHover,WebElement elementToClick) {
	Actions action = new Actions(driver);
Thanks for watching the video and reading the post !!! Please leave your comments and let me know if there is anything needed to be improved in this post. Thanks, Karthik KK

Post Author: Karthik kk

21 Replies to “Mouse hover and click in Selenium”

  1. Thanks for simple n nice tutorial on mouse hover and click. Can we perform all 3 operations in one action? The first hover u did separately, can that also be combined with the next hoverandclick function?

    1. Yes you can. To make more generic, you can write all the operation by first getting the WebElement via List and then you can call them one by one to perform operation. This makes you to reduce your number of parameters passed.

      Hope this will help.

      Karthik KK

  2. That will not work in Safari (I know…..I tried). Here’s what will work everywhere (if there is jQuery available).

    JavascriptExecutor js = null;

    String selector = “”;

    String jquery1 = “jQuery(\”” + selector + “\”).eq(0).mouseover();”;

    js = (JavascriptExecutor) driver;


    After that you can click on the newly visible items. My code for that is as follows (they are links):

    WebDriverWait wait = new WebDriverWait(driver, 50);
    WebElement element = null;

    try {
    element = wait.until(ExpectedConditions
    element = wait.until(ExpectedConditions.elementToBeClickable(By

      1. Hi Jeff/karthik,
        I want mouse over on a menu item and select sub menu item in safari browser. As you said, it is not happening using Actions class in selenium.
        Tried in internet and found your comment here and got a doubt in your solution.
        In my case, selector should be menu item and How to give value for selector here?
        I am new to jquery. Please help

  3. Hi,

    I would like to run more than 1 test methods in single webdriver(In one browser without closing that). Please let me know how to do so. I tried to set as

    but no use.

    1. You mean running test in parallel ?

      If yes, then you should use selenium grid, which you can find from here

      If your question is just to run multiple test sequentially on same browser, then its very simple. Just dont close your browser once your first test is completed and call second test method, it will run in same browser !!!

      I don’t think the attribute you are using in your code will help.

      Karthik KK

  4. package pmt_adminlogin;

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.interactions.Actions;

    public class Login {

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

    FirefoxDriver driver = new FirefoxDriver();
    // open URL

    //Enter USERNAME
    // Enter PASSWORD

    //Click on LOGIN


    class b2c{
    public void mouseover() throws InterruptedException{
    FirefoxDriver driver = new FirefoxDriver();

    WebElement menu = driver.findElement(By.xpath(“//*[@id=’b2ctour_block’]/ul/li[4]/a”));
    WebElement SUBMenu = driver.findElement(By.xpath(“//*[@id=’b2ctour_block’]/ul/li[4]/a”));

    Actions action = new Actions(driver);

    I want to login fast the site then mouse over on B2C menu and then click on Submenu Quick tour,But it i run this code it is only login the site then i is stop ,so can you help me on that.

    1. Is it something to do with page wait ?

      Did you tried waiting for the page to load using implicit or explicit wait ?

      Karthik KK

      1. Hi karthik,

        I also face the same issue. I tried to click on hdfc bank site – product – loans- car loan. when I tried using move to element method it shows offset value error. Offset within element cannot be scrolled into view. (0,0). Plz check the issue. I am stuck.

  5. Hey Karthik, really appreciate your efforts. It helps alot. But i have few doubts regarding pop ups.
    I have a side menu bar which opens only after hovering, and after that i need to click on one of the options on main page. But when i use the Actions method to hover and select, the control doesnt release because of which the side bar doesnt shrink (The options i need to click is visible only once the side bar shrinks again after hovering and selecting the option). Kindly share the code so that i can move the control from the side bar to the main page.

    Waiting for your reply. Thank you in advance.

  6. Hi Karthik,

    Thanks for sharing vidoe.
    I am trying the same scenario in this site(
    I wanted to mouse over Clothing & Jewellery and click on Activewear Link.
    Sample Code:
    WebElement menu = driver.findElement(By.xpath(“//*[@id=”wrapper”]/header/nav/div[2]/div[1]/div/div/ul/li[1]/a/span”));
    WebElement Childmenu = driver.findElement(By.xpath(“//*[text() = ‘Activewear ‘]”));
    Hover(driver, menu);
    HoverAndClick(driver, menu, Childmenu);
    But not click on Activewear link.
    Can you please help?


    1. try{
      WebElement element = driver.findElement(By.xpath(“(//a[@title=’Clothing & Jewellery’])[1]”));
      Actions action = new Actions(driver);
      Actions action1 = new Actions(driver);
      WebElement element1 = (new WebDriverWait(driver, 40))

  7. Kartik
    Thanks a lot for sharing such a useful video.
    I want to see an example where you can do both hover on an element
    can you or anyone please share example like that

  8. Hi,
    This is Amit.I want to automate a Canvas Graph which contains multiple bars and hover on each bar (which shows some data on UI).In the DOM it shows like for the entire Graph

    so can you tell me how to automate this one by python-selenium

    1. I am not very sure with Selenium, since all these graph validations can better be done with Visual tools like Sikuli, Percy etc, I have a video on the same for Cypress + Percy which does exactly the same thing

      Karthik KK

      1. Hi,
        how to do this mouseover & mouse click in selenium c# with visual studio 2015.
        i tried many time’s but i cant able to get output like this. Help me to clear this issue.

Leave a Reply

Your email address will not be published. Required fields are marked *