Keyboard and Mouse events in selenium are handled by Advanced Actions Interactions API. This API primarily has two classes Action and Actions. These are the classes that enable us to automate keyboard and mouse events. Mentioned below are some of the most commonly used events:

Mouse EventsKey Board Events
doubleClick() keyUp(modifier_key)
contextClick() keyDown(modifier_key)
clickAndHold() sendKeys(onElement, charsequence)
dragAndDrop(source, target)
dragAndDropBy(source, x-offset, y-offset)
moveByOffset(x-offset, y-offset)
moveToElement(toElement)
release()

Mouse Events:

1. doubleClick(): It performs double click on the current mouse location.

1
2
3
4
5
6
//Locating the Element
WebElement element=driver.findElement(By.id("btn"));

//Double click
Actions action = new Actions(driver);
action.doubleClick(element).perform();

2. contextClick(): Performs Right Click on the current mouse location.

1
2
3
4
5
6
//Locating the Element
WebElement element=driver.findElement(By.id("btn"));

//Right or Context Click
Actions action = new Actions(driver);
action.contextClick(element).perform();

3. clickAndHold(): Click and Holds without releasing on the current mouse location.

1
2
3
4
5
6
//Locating the Element
WebElement element=driver.findElement(By.id("btn"));

//Click and Hold
Actions action = new Actions(driver);
action.clickAndHold(element).perform();

4. dragAndDrop(source, target): Relocates the element from source to target. It clicks and holds the element at the source and releases it at target.

1
2
3
4
5
6
7
8
9
//Source
WebElement From = driver.findElement(By.id("drag"));

//target
WebElement To = driver.findElement(By.id("drop"));
 
//Drag and Drop
Actions action = new Actions(driver);
action.dragAndDrop(From, To).build().perform();

5. dragAndDropBy(source, x-offset, y-offset): Click and holds at the location of source element and moves by a given offset, followed by the release of the mouse.

1
2
3
4
5
6
//Locate element to be dragged
WebElement eleDrag = driver.findElement(By.id("btnId");

//Drag and Drop via x-Offset by 200 pixel and y-Offset by 10 pixel
Actions action = new Actions(driver);
action.dragAndDropBy(eleDrag, 200, 10).build().perform();

6. moveByOffset(x-offset, y-offset): Moves the mouse from its current position (or 0,0) by the given offset.

1
2
3
4
5
6
//Locate element to be moved
WebElement eleDrag = driver.findElement(By.id("btnId");

//Move the element x-Offset by 150 pixel and y-Offset by 100 pixel
Actions action = new Actions(driver);
action.clickAndHold(eleDrag).moveByOffset(150, 100).release().build().perform();

7. moveToElement(toElement): Moves the mouse to the specified element.

1
2
3
4
5
6
//Locate element
WebElement element = driver.findElement(By.id("btnId");

//Move To specified element
Actions actions = new Actions(driver);
actions.moveToElement(element);

8. release(): Releases the depressed left mouse button at the current mouse location.

1
2
3
4
5
6
//Locate element
WebElement element = driver.findElement(By.id("btnId");
 
//click and Hold an element and then Releasing it
Actions actions = new Actions(driver);
action.clickAndHold(element).release().build().perform();

 

Keyboard Events:

1. sendKeys(onElement, charsequence): Using this method, we can send a series of keystrokes on a particular element. It has two parameters:
onElement – The element on which the keystrokes are being sent to. Eg. text fields.
charsequence – string value to be sent to the respective element.

1
2
3
4
5
6
//Locating Text Field
WebElement element = driver.findElement(By.id("email");
 
//Sending the character sequence "Hello" to the text field
Actions actions = new Actions(driver);
actions.sendKeys(element, "Hello").perform();

2. keyDown(modifier_key): Performs a Keypress for modifier key. It has one parameter:
modifier_key – The modifier keys are Keys.ALT, Keys.SHIFT, or Keys.CONTROL

3. keyUp(modifier_key): Performs a Key release. It has one parameter:
modifier_key – The modifier keys are Keys.ALT, Keys.SHIFT, or Keys.CONTROL
 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class SendKeys {

    public static void main(String[] args) throws InterruptedException {

        // Telling Selenium to find Chrome Driver
        System.setProperty("webdriver.chrome.driver", "C:\\selenium\\chromedriver.exe");

        // Initialize browser
        ChromeDriver driver = new ChromeDriver();

        // Launch Facebook
        driver.get("http://facebook.com/");

        // Sleep
        Thread.sleep(2000);

        // Locating the Text Box
        WebElement element = driver.findElementById("email");

        // Clearing the Element Textbox
        element.clear();

        // Sleep
        Thread.sleep(2000);

        // Writing hello in Caps in email field
        Actions actions = new Actions(driver);
        actions.keyDown(element, Keys.SHIFT).sendKeys(element, "hello").keyUp(element, Keys.SHIFT)
.build().perform();
       
        //Checking whether the value has been entered in Caps or not
        if (element.getAttribute("value").equals("HELLO")) {
            System.out.println("Success");
        } else {
            System.out.println("Failure");
        }
       
        // Close the browser
        driver.quit();

    }

}