Handle Mouse and Keyboard actions in Selenium

In day to day life, for any work on the computer, we use Mouse and keyboard most of the time. In this tutorial, we are going to learn how to use mouse and keyboard keys with webdriver.

Action class supports only supporting keys like : Keys.SHIFT, Keys.ALT or Keys.CONTROL. If the provided key is none of those, IllegalArgumentException will be thrown by selenium webdriver

Actions Class in Webdriver

org.openqa.selenium.interactions.Actions or simply Actions class supports the user to perform all the operations related to mouse and keyboard keys from the keyboard.

Actions class extends Object class directly and TouchActions class extends Actions class. It follows multilevel inheritance. Actions class will not help in window switch and frame switching operations

Implements the builder pattern: Builds a CompositeAction containing all actions specified by the method calls.

Composite Actions : User can create a series of operations using Actions class and force them to work one by one continuously using a actions builder (build() method from the Actions class helps the user to perform the pattern building).

Explanation : The Actions class allows us to build a chain of actions that we would like to perform.
This means that we can build up a nice sequence of operations, for example, Press Shift key and type something and then release, or if we want to work checkboxes, we can press CTRL and then click the required checkboxes

How to use Actions class in Selenium webdriver
1. Open Browser by setting system Property for the driver server exe file.


                        		// set chrome driver exe path
		System.setProperty("webdriver.chrome.driver", "C:/path/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
                        	


2. Open required webpage url :


                        		driver.get("https://chercher.tech/");
                        	


3. Create an object to Actions class to access all the non-static methods, Actions class must be imported from import org.openqa.selenium.interactions.Actions;


                        		Actions act = new Actions(driver);
                        	


5. Use required method from actions class and pass element as parameter if parameter required

Complete program for Actions class in Selenium mouse-keyboard-actions-class-selenium-webdriver

How to handle Multiple Value Dropdown

Operations Supported by Actions Class


Mouse Actions:
Mouse Actions on Selenium helps the user to perform all the operations related to a mouse, like : clicking, dragging, moving, clicking and dragging, hovering, double clicking, right-clicking.

  • build()
  • click()
  • clickAndHold()
  • contextClick()
  • doubleClick()
  • dragAndDrop(WebElement source, WebElement target)
  • moveByOffset(int xOffset, int yOffset)
  • moveToElement(WebElement target)
  • moveToElement(WebElement target, int xOffset, int yOffset)
  • pause(long pause)
  • perform()
  • release()

KeyBoard Actions

Selenium Webdriver Provides Api for keyboard as well through action class, with help of it we can emulate keyboard but here selenium webdriver limits the keys to only CTRL, ALT, Shift with keyUp, KeyDown methods

But with the help of the Sendkeys method, we can still automate all the keys present in the keyboard, we will see how to handle all the keys in keyboard on Keyboard Action on Selenium Webdriver tutorial

  • sendKeys(java.lang.CharSequence... keys)
  • keyUp(java.lang.CharSequence key)
  • keyDown(java.lang.CharSequence key)

JavascriptExecutor

Context Click or Right Click in Selenium

We can right click an element or an webpage using Actions class in selenium, contextClick method in Actions class helps us to do a right click on the elements.

We may have scenarios like right-clicking a link and open it in a new tab, sometimes we may need to verify context-click menu options.

Steps to Context-click in selenium :

  • Open browser and navigate to url
  • Find the Webelement that you want to right click
  • Store the Webelement as an element
  • Create an object/instance for Actions class from selenium interactions passing driver as a parameter
  • Use contextClick() method from the Actions class object 'act' and pass the stored webelement as a parameter.
  • Call the perform method from the Actions class to complete the operation

Scenario : Context click this Handle Mouse Actions

Solution for the scenario :
1. Open browser and Url : https://chercher.tech/java/mouse-keyboard-actions-class-selenium-webdriver


											// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		// open firefox
		WebDriver driver = new FirefoxDriver();
										


2. Find the Selenium Webdriver link, and store it


												//find the element to context click
		WebElement element = driver.findElement(By.linkText("Selenium Webdriver"));
										


3. Create an object for Actions class


											// Create an object for Actions class
		Actions act = new Actions(driver);
										


4. Call the contextClick() method from the Actions class object, and pass the stored element.


											// call the contextClick method and pass the stored parameter
		act.contextClick(element);
										


5. Call the perform method from the Actions class object.


											// complete the operation
		act.perform();
										

Complete code to context-click the link

public class MouseRightClick {
	public static void main(String[] args) {
		// set the geckodriver.exe property
		System.setProperty("webdriver.gecko.driver", "C:/~/geckodriver.exe");
		// open firefox
		WebDriver driver = new FirefoxDriver();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		// navigate to url
		driver.get("https://chercher.tech/java/mouse-keyboard-actions-class-selenium-webdriver");
		//find the element to context click
		WebElement element = driver.findElement(By.linkText("Selenium Webdriver"));
		// Create an object for Actions class
		Actions act = new Actions(driver);
		// call the contextClick method and pass the stored parameter
		act.contextClick(element);
		// comlete the operation
		act.perform();
	}
}
										

If you try to click an element which is not displayed on the screen, selenium throws MoveTargetOutOfBoundsException

Convert BYTES screenshot to Image in selenium

Drag and drop with Actions class

Drag and dropping an element in webpages is a rare scenario, but it does exist. dragAndDrop() Method in Actions class helps the selenium user to perform this task.

Steps to perform Drag and Drop:

  • Open the browser and navigate to Url
  • Find the element, which you want to drag, it is also called as a source.
  • Find another element, to which you want to drag the first element, it is also called target.
  • Create an object for Actions class, pass driver as a parameter
  • Call the dragAndDrop() method from the Actions class object, pass source as the first parameter, target as the second parameter.
  • Call the perform() method to complete the operation

Scenario to do Drag and drop :
Navigate to https://chercher.tech/java/drag-and-drop-example.html and drag the S-W icon from the first div to the second div.

Solution for the scenario :
1. Open browser and navigate to url 2. Find the source element and store it as source (icon is source)


											//find the source element
		WebElement source = driver.findElement(By.xpath("//img"));
										


3. Find the target element and store it as target(div2 is the target)


											WebElement target = driver.findElement(By.id("div2"));
										


4. Create an object for Actions class


											// Create an object for Actions class
		Actions act = new Actions(driver);
										


5. Call dragAndDrop() method from the actions class, pass source and target.


											// perform drag and drop
		act.dragAndDrop(source, target).perform();
										


6. Complete the task by calling perform.
Complete code to Drag and Drop click the link

// PLEASE DO WRITE IMPORT STATEMENTS
public class MouseDragAndDrop {
	public static void main(String[] args) {
		// set chrome driver exe path
		System.setProperty("webdriver.chrome.driver", "C:/~/chromedriver.exe");
		WebDriver driver = new ChromeDriver();
		driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);
		// navigate to url
		driver.get("https:///chercher.tech/practice/drag-drop-example");
		//find the source element
		WebElement source = driver.findElement(By.xpath("//img"));
		WebElement target = driver.findElement(By.id("div2"));
		// Create an object for Actions class
		Actions act = new Actions(driver);
		// perform drag and drop
		act.dragAndDrop(source, target).perform();
	}
}
										

Multi-level Inheritance in Java and Selenium

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
  • santhosh
    HI KarthiQ,
    i am not able to download the program code any ,extension should i install
    Reply