How To Perform Mouse Actions

In this article, we will learn how to perform the mouse actions in Selenium.

To perform mouse actions in selenium, we need to create the object of “Actions” class and pass the webdriver as argument.

Actions act = new Actions(driver);

To perform such actions, Selenium provides various methods as below:

  • 1. click() : To perform left click action of mouse.
  • 2. doubleClick() : To perform the double Click operation of mouse.
  • 3. contextClick() : To perform right click operation of mouse.
  • 4. moveToElement(webelement) : To move the pointer to specific element of the application.
  • 5. dragAndDrop(webelement, webelement) : To perform drag and drop operation.
  • 6. perform() : This method is used to execute the action on the browser. (Compulsory)
  • 7. build() : This method is used to combine(connect) multiple actions in a single statement.

1. click() :
This method is use to perform left click action of mouse.

CODE SNIPPET
        
	WebElement savebtn = driver.findElement(By.id("save"));
    
	Actions act = new Actions(driver)
    
	act.click(savebtn).perform();
	
	// or simply we can write as
	
	savebtn.click;
	 

2. doubleClick() :
This method is use to perform the double Click operation of mouse.

CODE SNIPPET
	WebElement doubleClickBtn = driver.findElement(By.id("Click"));
	
	Actions act = new Actions(driver);

	act.doubleClick(doubleClickBtn).perform();
		

3. contextClick() :
This method is use to perform right click operation of mouse.

CODE SNIPPET
	WebElement rightClickBtn = driver.findElement(By.id("clickable"));
	
	Actions act = new Actions(driver);

	act.contextClick(rightClickBtn).perform();
		

4. moveToElement(webelement) :
This method is use to move the pointer to specific element of the application.

CODE SNIPPET
	WebElement moreField = driver.findElement(By.id("more"));
	
	Actions act = new Actions(driver);

	act.moveToElement(moreField).perform();                                
		

5. dragAndDrop(webelement, webelement) :
This method is use to perform drag and drop operation.

CODE SNIPPET
	
	WebElement image = driver.findElement(By.id("gallery"));

	WebElement trash = driver.findElement(By.id("trash"));
			
	Actions act = new Actions(driver);
	
	act.dragAndDrop(images, trash).perform();

#How_to_perform_mouse_actions_in_selenium_with_example #click_method #doubleClick #contextClick #moveToElement #dragAndDrop #perform #build

(New page will open, for Comment)

Not yet commented...