Handle Mouse and Keyboard actions in Selenium Webdriver

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 selenium 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 throwed by selenium webdriver.

Actions Class in Selenium Webdriver

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

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 frome switching operations

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

Composite Actions : User can create series of operations using Actions class and force them 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 ti 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 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 webdriver helps user to perform all the operations related with mouse
Mouse Operations like : clicking, dragging, moving, clicking and dragging, hovering, double clicking, right clicking.

1. build()
2. click()
3. clickAndHold()
4. contextClick()
5. doubleClick()
6. dragAndDrop(WebElement source, WebElement target)
7. moveByOffset(int xOffset, int yOffset)
8. moveToElement(WebElement target)
9. moveToElement(WebElement target, int xOffset, int yOffset)
10. pause(long pause)
11. perform()
12. 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 help of Sendkeys method we can still automate all the keys present in the keyboard, we will see how handle all the keys in keysboard on Keyboard Action on Selenium Webdriver tutorial

1. sendKeys(java.lang.CharSequence... keys)
2. keyUp(java.lang.CharSequence key)
3. keyDown(java.lang.CharSequence key)

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 click a link and open it in new tab, sometimes we may need to verify context click menu options.

Steps to Context click in selenium :
1. Open browser and navigate to url
2. Find the Webelement that you want to right click
3. Store the Webelement as element
4. Create object/instance for Actions class from selenium interactions passing driver as parameter
5. Use contextClick() method from the Actions class object 'act' and pass the stored webelement as parameter.
6. 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 : http://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 object for Actions class


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


4. Call contextClick method fom 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("http://chercher.tech/java/mouse-keyboard-actions-class-selenium-webdriver");
		//find the element to context click
		WebElement element = driver.findElement(By.linkText("Selenium Webdriver"));
		// Create 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 webdriver throws org.openqa.selenium.interactions.MoveTargetOutOfBoundsException

Convert BYTES screenshot to Image in selenium

Drag and drop with Actions class

Drag and dropping an element in webpages is 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:
1. Open the browser and navigate to Url
2. Find the element, which you want drag, it is also called as source.
3. Find the another element, to which you want to drag the first element, it is also called target.
4. Create object for Actions class, pass driver as parameter
5. Call the dragAndDrop method from the Actions class object, pass source as first parameter, target as second parameter.
6. Call perform method to complete the operation

Scenario to do Drag and drop :
Navigate to http://chercher.tech/java/drag-and-drop-example.html and drag the S-W icon from the first div to 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 target)


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


4. Create object for Actions class


											// Create 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("http:///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 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

 
Join My Facebook Group
Join Group
 

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions
Copyright © CherCher Tech