Italian Trulli

How to Handle Mouse Action in Selenium Using Java

Test Tribe
Handling Mouse Action In Selenium Webdriver

In this tutorial, we are going to learn about action class in selenium and other mouse events like mouse hover, mouse drag and drop, double click, single click etc.

  • Mouse and Keyboard Event 
Mouse Event-List of mouse events is given below.
  • clickAndHold()
  • contextClick()
  • doubleClick()
  • dragAndDrop()
  • dragAndDropBy()
  • moveByOffset()
  • moveToElement()
  • release()
Keyboard Event- A list of Keyboard events is given below.
  • keyDown()
  • keyUp()
  • sendkeys()
Action Class in Selenium -Mouse Hover and keyboard Event 

Action class in selenium webdriver
Action class in selenium is a predefined class which is used to handle the mouse and keyboard event.

  • How to Perform Mouse Hover in selenium webdriver using java?
Mouse hover in selenium - moveToEelement()
To perform mouse hover in selenium webdriver there is a predefine Action class which provides a method called moveToElement (), By using this method we can move the mouse to our target.

Selenium Code -
Code to handle mouse hover in selenium given below 

WebElement ele = driver.findElement(By.xpath("<xpath>"));
 //Creating object of an Actions class
Actions action = new Actions(driver);
 // mouse hover action on the target element.
  • How to Perform Right-Click in selenium webdriver using java?

Right click in selenium - contextClick ()
 To right-click on a web element in selenium we use action class, which provides a method called contextClick(). By using this method we can right-click on the current location and right-click on the web element.

Selenium Code-
Selenium code to perform right-click given below

// How to right click in selenium webdriver 
Actions actions = new Actions(driver);
WebElement elementLocator = driver.findElement(By.xpath("//span[@class=\"context-menu-one btn btn-neutral\"]"));

  • How to perform Double click in selenium webdriver using java?
Double Click in selenium - doubleClick()
To double click on a web element in selenium we use action class, which provides a predefined method called doubleClick () . by using this method we can perform double click action in selenium.

Selenium Code 
Selenium code to perform double click given below 

// How to double click in selenium webdriver 
Actions actions = new Actions(driver);
WebElement elementLocator=driver.findElement(By.xpath("//button[@ondblclick=\"myFunction()\"]"));
System.out.println("Done- double click ");

  • How to Perform drag and drop action in selenium webdriver using java?
Drag and drop in selenium - dragAndDrop()

Selenium Code 
Selenium code to perform mouse drag and drop action 

 //Source WebElement on which drag and drop operation 
   WebElement from = driver.findElement("sourceImage"));  
    //Targert WebElement to which the above object is dropped  
        WebElement to = driver.findElement("targetDiv"));  
    //Creating object of Actions class 
        Actions act = new Actions(driver);  
    //Performing the drag and drop action  

Share Your Knowledge With us -
Mail your content

Hope !!! Above tutorial on handling mouse action in selenium is helpful for you ...
Team ,
QA acharya

Tags: mouse hover in selenium , handling mouse in selenium webdriver using java , how to handle mouse action in selenium , Mouse action in selenium , keyboard and mouse event using selenium 

Handling Mouse Action in selenium webdriver

Post a Comment