Skip to content
CodeKruPro

Mouse Hover Action

51/70

Mouse Hover Action

The Actions class provides advanced user interactions. moveToElement() simulates mouse hover.

On the Buttons page, the hover button (id="colorChangeOnHover") changes its background color when hovered.

Task:

  1. Navigate to https://testkru.com/Elements/Buttons
  2. Find the hover button
  3. Print its text
  4. Use Actions to hover over it
  5. Print "Hovered successfully"

Expected output:

Button text: Hover on me
Hovered successfully

About this lesson

Mouse hover in Selenium Java with Actions moveToElement()

Learn Selenium JavaAdvancedActions Class

How it works

Hovering has no WebElement method. You build it with the Actions class: new Actions(driver).moveToElement(element).perform() moves the mouse pointer to the centre of the element, and the page reacts exactly as it would to a real pointer, with :hover styles and mouseover handlers.

Actions are queued, not sent. moveToElement() and build() only add steps to a sequence, and nothing reaches the browser until perform(). Without it the hover button on the Buttons page keeps its original background, rgba(255, 186, 0, 1).

The pointer stays where you left it. After the hover the button's background settles at rgba(8, 111, 109, 1) and keeps that colour until the mouse moves elsewhere. The page animates the change over 0.15 s, so a value read immediately after perform() can be an in-between colour; wait for the final value before asserting it.

About the assignment

The Buttons page has a button that changes colour while the mouse is over it. You read its label, hover over it with Actions, and report that the hover ran.

What you will do

  1. Find the hover button by the id from the task and print its text after the label Button text:.
  2. Create an Actions object for the driver, call moveToElement() with the button and finish with perform().
  3. Print the confirmation line from the task.

Common mistakes

  • Forgetting perform()

    The code compiles and runs, but the mouse never moves. Every Actions chain must end in perform().

  • Asserting the hover without checking the effect

    Printing a line proves the code ran, not that the page reacted. In a real test, read getCssValue("background-color") after the hover and compare it.

  • Hovering over something out of reach

    moveToElement() scrolls an element below the fold into view on its own, but an element laid out outside the page, like the sidebar links at a negative x position in the default 780x580 window, throws MoveTargetOutOfBoundsException. Resize the window so it is on screen.

In a real test suite

Hover opens dropdown menus, shows tooltips and reveals row actions such as edit and delete icons. Tests hover, wait for the revealed element to be visible, then click it, often all in one Actions chain.

Lesson 50 sized the window. The next two lessons use the same Actions class for double-clicks and right-clicks on this page.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Elements/Buttons");
        
        // TODO: Find the colorChangeOnHover button
        // TODO: Print its text
        // TODO: Use Actions to hover over it
        // TODO: Print "Hovered successfully"
        
        driver.quit();
    }
}