Actions

The Actions class is part of org.openqa.selenium.interactions package. It provides a way to perform complex user interactions such as mouse and keyboard actions. It is particularly useful for handling scenarios where simple interactions like clicks or text input are not sufficient, and more advanced interactions are needed. Actions class implements the builder pattern: Builds a CompositeAction containing all actions specified by the method calls. It will Call method perform() at the end of the method chain to actually perform the actions.

Following are some of the commonly used methods.

  • sendKeys
  • moveToElement
  • contextClick
  • doubleClick
  • dragAndDrop
  • keyDown
  • keyUp

sendKeys

This method will sends keys to the active element or the web element provided. This method will return a Actions object, hence you can chain multiple actions method to this method as per your requirements.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/setData/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement firstNameElem = driver.findElement(By.name("fname"));
Actions actions = new Actions(driver);
actions.sendKeys(firstNameElem, "Test Automation ").sendKeys("Studio").perform();
driver.quit();

moveToElement

This method will moves the mouse to the middle of the element. The element is scrolled into view and its location is calculated using getClientRects.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/actions/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement idElem = driver.findElement(By.id("idMenu"));
Actions actions = new Actions(driver);
actions.moveToElement(idElem).click().perform();
driver.quit();

contextClick

This method will performs a context-click at middle of the given element. First it will performs a mouseMove to the location of the element, then will perform the context-click.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/actions/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement contextClickElem = driver.findElement(By.id("action3"));
Actions actions = new Actions(driver);
actions.contextClick(contextClickElem).perform();
driver.quit();

doubleClick

Performs a double-click at middle of the given element. First it will performs a mouseMove to the location of the element, then will perform the double-click.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/actions/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement doubleClickElem = driver.findElement(By.id("action1A"));
Actions actions = new Actions(driver);
actions.doubleClick(doubleClickElem).perform();
driver.quit();

dragAndDrop

This method will take two Web Element as parameter, ie source element and a destination element. This method performs click-and-hold at the location of the source element, then moves to the location of the target element, then releases the mouse.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/actions/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement sourceElem = driver.findElement(By.xpath("//ul[@id='sortable1']/li[1]"));
WebElement destElem = driver.findElement(By.id("sortable2"));
Actions actions = new Actions(driver);
actions.dragAndDrop(sourceElem, destElem).perform();
driver.quit();

keyUp and keyDown

The keyDown method will perform a modifier key press after focusing on an element. The keyUp method will performs a modifier key release after focusing on an element.

Java
C#
Python
Javascript
Kotlin
Copy
WebDriver driver = new ChromeDriver();
driver.get("http://localhost:8080/demo/actions/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(30));
WebElement contextClickelem = driver.findElement(By.id("action3"));
contextClickelem.sendKeys("Welcome");
Actions actions = new Actions(driver);
actions.keyDown(contextClickelem, Keys.CONTROL).keyUp(contextClickelem, "a").keyDown(contextClickelem, Keys.DELETE).perform();
driver.quit();