How to handle mouse in selenium - Action class in selenium -Mouse hover and keyboard event

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

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


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

Selenium Code -
To handle mouse hover in selenium code 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 an web element in selenium we use action class , which provide a method called contextClick() .By using this method we can right click on current location and right click on 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\"]"));
actions.contextClick(elementLocator).perform();
System.out.println("clicked");

  • How to perform Double click in selenium webdriver using java ?
Double Click in selenium - doubleClick()
To double click on an web element in selenium we use action class, which provide a predefine 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 right 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 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(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();

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

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




Handling Mouse Action in selenium webdriver


Post a Comment

0 Comments