Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Tech Solutions
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • E-Commerce Demo Application
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

Selenium Automation Testing

  • Introduction to Automation Testing
  • Introduction to Selenium
  • Advantages and Disadvantages of Selenium WebDriver
  • Locators in Selenium
  • How to launch the browsers ? 
  • Browser Navigation Commands in Selenium
  • Handling the textbox and button using selenium
  • Handling the radio button and checkbox using selenium
  • Handling the drop downs and multi-select dropdown
  • Validate the text validation and attributes values. 
  • Handling Images
  • Handling Mouse Events
  • Handling Keyboard Events
  • Handling Alert Popup
  • Handling iFrames
  • Handling Multiple Browsers or Tabs
  • Types of wait statement in selenium
  • How to take screenshots?
  • How to upload File in Selenium?
  • How to handle scroll function using JavaScriptExecutor?
  • How to handle basic functionality using JavaScriptExecutor?
  • How to handle advanced functionality using JavaScriptExecutor?
  • How to automate CAPTCHA?
  • How to handle cookies in selenium
View Categories
  • Home
  • Tutorials
  • Selenium Automation
  • Selenium Automation Testing
  • Handling Mouse Events

Handling Mouse Events

Handling Mouse Events

How to handle double click?  

Key Points:

  • Actions class is used for advanced user interactions (mouse, keyboard).
  • .doubleClick(WebElement) method simulates a double-click on the given element.
  • Always end the action with .perform() to execute it.

Sample Example:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

public class DoubleClickExample {

    public static void main(String[] args) {

        // Set the path to your chromedriver

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            // Navigate to the webpage

            driver.get("https://example.com/double-click-demo");

            // Locate the element to double click

            WebElement elementToDoubleClick = driver.findElement(By.id("doubleClickBtn"));

            // Create Actions object

            Actions actions = new Actions(driver);

            // Perform double click

            actions.doubleClick(elementToDoubleClick).perform();

            // Add a pause to see the result (optional)

            Thread.sleep(2000);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            driver.quit();

        }

    }

}

How to handle right click?   

Key Points:

  • Use actions.contextClick(WebElement) to simulate a right-click on the target element.
  • Use .perform() to execute the action.
  • This is commonly used to test context menus or custom right-click actions.

Sample Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

public class RightClickExample {

    public static void main(String[] args) {

        // Set the path to your chromedriver

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            // Navigate to the webpage

            driver.get("https://example.com/right-click-demo");

            // Locate the element to right click

            WebElement elementToRightClick = driver.findElement(By.id("rightClickBtn"));

            // Create Actions object

            Actions actions = new Actions(driver);

            // Perform right click (context click)

            actions.contextClick(elementToRightClick).perform();

            // Add a pause to see the result (optional)

            Thread.sleep(2000);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            driver.quit();

        }

    }

}

How to drag and drop?   

Key Points

  • Use .dragAndDrop(source, target) for most cases.
  • If drag-and-drop does not work due to JS events, try clickAndHold and moveToElement.
  • Always end with .perform().

Sample Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

public class DragAndDropExample {

    public static void main(String[] args) {

        // Set the path to your chromedriver

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            // Open a demo drag and drop site

            driver.get("https://demoqa.com/droppable");

            // Locate the source (draggable) and target (droppable) elements

            WebElement sourceElement = driver.findElement(By.id("draggable"));

            WebElement targetElement = driver.findElement(By.id("droppable"));

            // Create Actions object

            Actions actions = new Actions(driver);

            // Perform drag and drop

            actions.dragAndDrop(sourceElement, targetElement).perform();

            // Optional: pause to see the result

            Thread.sleep(2000);

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            driver.quit();

        }

    }

}

Alternative: Click-Hold-Move-Release (Sometimes works better)

Some web apps require a more manual drag-and-drop:

actions.clickAndHold(sourceElement)

       .moveToElement(targetElement)

       .release()

       .build()

       .perform();

How to verify tooltips? 

Verifying tooltips in Selenium WebDriver involves checking the text that appears when you hover over an element.
Most tooltips are shown in two ways:

  1. As a title attribute (default browser tooltip)
  2. As a custom element (shown only after hover, e.g., with JavaScript frameworks)

Here’s how to verify both types:

1. Verifying Browser Default Tooltip (title attribute)

Sample Program: 

WebElement element = driver.findElement(By.id("myElementId"));

String tooltipText = element.getAttribute("title");

// Now verify tooltip text

if (tooltipText.equals("Expected tooltip text")) {

    System.out.println("Tooltip verified!");

} else {

    System.out.println("Tooltip does not match!");

}

Tip:

No need to hover. The tooltip text is stored in the title attribute, so you can get it directly.

2. Verifying Custom Tooltip (Appears After Hover)

Steps:

  • Hover over the element to make the tooltip visible.
  • Locate the tooltip element that appears.
  • Get and verify the tooltip text.

Sample Program:

import org.openqa.selenium.interactions.Actions;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class TooltipVerification {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            driver.get("https://example.com/tooltip-demo");

            WebElement element = driver.findElement(By.id("customTooltipBtn"));

            // Hover over the element to display tooltip

            Actions actions = new Actions(driver);

            actions.moveToElement(element).perform();

            // Wait for tooltip to appear (adjust locator as per your app)

            Thread.sleep(2000); // Replace with WebDriverWait for production code

            WebElement tooltip = driver.findElement(By.cssSelector(".tooltip-class")); // Use the correct selector

            String tooltipText = tooltip.getText();

            if (tooltipText.equals("Expected tooltip text")) {

                System.out.println("Tooltip verified!");

            } else {

                System.out.println("Tooltip does not match!");

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            driver.quit();

        }

    }

}

Tip:

For dynamic tooltips, always use WebDriverWait instead of Thread.sleep() for better reliability.

How to perform mouse hover ?

To perform mouse hover actions in Selenium WebDriver, you use the Actions class and its .moveToElement() method.

This is often used for revealing dropdown menus, tooltips, or triggering JavaScript events. 

Key Points

  • Use actions.moveToElement(element).perform(); to simulate mouse hovering.
  • After hovering, you can interact with elements that appear (like submenus).
  • For complex scenarios, chain more actions:
    .moveToElement(element).click().build().perform();

Sample Program:

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.interactions.Actions;

public class MouseHoverExample {

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

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        try {

            driver.get("https://demoqa.com/menu/"); // Example site with menu on hover

            // Locate the element to hover over

            WebElement menuElement = driver.findElement(By.linkText("Main Item 2"));

            // Create Actions object

            Actions actions = new Actions(driver);

            // Perform mouse hover

            actions.moveToElement(menuElement).perform();

            // Pause to observe the effect (optional)

            Thread.sleep(2000);

        } finally {

            driver.quit();

        }

    }

}

Mouse Hover and Then Click Example

Suppose you need to hover and then click a submenu item:

Sample Program:

WebElement menu = driver.findElement(By.id("menu"));

WebElement submenu = driver.findElement(By.id("submenu"));

Actions actions = new Actions(driver);

actions.moveToElement(menu).moveToElement(submenu).click().build().perform();

Tip:

  • Always use proper waits if elements are loaded dynamically!
  • Let me know if you need a Python example or how to combine hover with waits.

Summary Table of Keyboard Actions

ScenarioCode Example
Type textelement.sendKeys(“text”)
Press ENTER/TAB/ESCelement.sendKeys(Keys.ENTER)
Keyboard shortcuts (Ctrl+A)actions.keyDown(Keys.CONTROL).sendKeys(“a”).keyUp(Keys.CONTROL).perform();
Clear fieldelement.clear() or element.sendKeys(Keys.CONTROL + “a”, Keys.DELETE)
Arrow key navigationelement.sendKeys(Keys.ARROW_DOWN)
Shift/Alt combinationsactions.keyDown(Keys.SHIFT)…
Submit formelement.sendKeys(Keys.ENTER) or element.submit()
Selenium Automation Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
Handling ImagesHandling Keyboard Events
© 2018 – 2025 Vinoth Tech Solutions Ltd (UK), Reg. No: 16489105