How to Handle Mouse Action in Selenium Using Java

.
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 that is used to handle the mouse and keyboard event.


How to Perform Mouse Hover in Selenium web driver using Java?

Mouse hover in selenium - moveToEelement()
To perform mouse hover in Selenium webdriver there is a predefined Action class that 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 is 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.
action.moveToElement(ele).perform();


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 an 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 is 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\"]"));
actions.contextClick(elementLocator).perform();
System.out.println("clicked");

How to perform Double click in selenium web driver 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()\"]"));
actions.doubleClick(elementLocator).perform();
System.out.println("Done- double click ");


How to Perform drag and drop action in Selenium web driver 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(By.id("sourceImage"));  
 
    //Targert WebElement to which the above object is dropped  
        WebElement to = driver.findElement(By.id("targetDiv"));  
           
    //Creating object of Actions class 
        Actions act = new Actions(driver);  
           
    //Performing the drag and drop action  
        act.dragAndDrop(from,to).build().perform();



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

Share Your Knowledge With us -
Mail your content qaacharya.in@gmail.com

Handling Mouse Action in selenium web driver

Post a Comment

0 Comments