What Is the Actions Class?

The Actions class is used in Selenium to perform complex user interactions such as:

  • Mouse movements
  • Right-click
  • Drag and drop
  • Keyboard operations

These are actions that cannot be performed using simple click() or sendKeys() methods on a WebElement.

Create an Actions object by passing the WebDriver instance.

Advertisement
 
Actions actions = new Actions(driver);
 

Right-Click (contextClick)

Perform a right-click operation using the contextClick() method of the Actions class.

 
public class RightClickExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.example.com");

        // Locate the element
        WebElement element = driver.findElement(By.id("elementId"));

        // Perform right-click
        Actions actions = new Actions(driver);

        actions.contextClick(element).perform();

        driver.quit();
    }
}
 

The following statement is also valid:

 
actions.contextClick(element).build().perform();
 

build() vs perform()

build()

  • Creates a composite action.
  • Used when multiple actions are chained together.
  • Returns an Action object.

perform()

  • Executes the defined action(s).
  • Executes them in the same order they were defined.

Single Action

Use perform() directly.

 
actions.moveToElement(element).perform();
 

Multiple Actions

Use build().perform().

 
actions.clickAndHold(element1)
       .moveToElement(element2)
       .release()
       .build()
       .perform();
 

Store an Action and Execute Later

 
Action dragAndDrop =
        actions.clickAndHold(element1)
               .moveToElement(element2)
               .release()
               .build();

dragAndDrop.perform();
 

Drag and Drop

Use the dragAndDrop() method.

 
public class DragAndDropExample {

    public static void main(String[] args) {

        System.setProperty("webdriver.chrome.driver", "path/to/chromedriver");

        WebDriver driver = new ChromeDriver();

        driver.get("https://www.example.com");

        // Locate source and target
        WebElement sourceElement = driver.findElement(By.id("source"));
        WebElement targetElement = driver.findElement(By.id("target"));

        // Perform drag and drop
        Actions actions = new Actions(driver);

        actions.dragAndDrop(sourceElement, targetElement)
               .build()
               .perform();

        driver.quit();
    }
}
 

The manual alternative is:

 
actions.clickAndHold(sourceElement)
       .moveToElement(targetElement)
       .release()
       .build()
       .perform();
 

This approach is useful when dragAndDrop() does not work correctly for a particular application.


Mouse Hover and Chained Actions

Use moveToElement() to hover over an element.

 
Actions actions = new Actions(driver);

actions.moveToElement(element1)
       .click()
       .moveToElement(element2)
       .click()
       .perform();
 

This is commonly used for multi-level menus.


Clicking a Menu Item in a Drop-Down Menu

For non-<select> dropdown menus:

Step 1: Locate the menu

 
WebElement dropdownMenu =
driver.findElement(By.id("dropdown-menu"));
 

Step 2: Expand the menu

 
dropdownMenu.click();
 

Step 3: Click the required menu item

 
WebElement menuItem =
driver.findElement(By.id("menu-item"));

menuItem.click();
 

For hover menus, replace the first click() with:

 
actions.moveToElement(dropdownMenu).perform();
 

Keyboard Operations

Keyboard operations are performed using the Actions class together with the Keys class.

 
Actions actions = new Actions(driver);

// Press ENTER
actions.sendKeys(Keys.ENTER).perform();

// Ctrl + A
actions.sendKeys(Keys.CONTROL, "a").perform();

// Arrow Down
actions.sendKeys(Keys.ARROW_DOWN).perform();

// Enter text
WebElement inputField =
driver.findElement(By.id("input-field"));

actions.sendKeys(inputField, "Hello, World!")
       .perform();
 

Performing Ctrl + A and Ctrl + C

Use keyDown() and keyUp().

 
actions.keyDown(Keys.CONTROL)
       .sendKeys("a")
       .sendKeys("c")
       .keyUp(Keys.CONTROL)
       .perform();
 
  • keyDown() presses and holds the modifier key.
  • sendKeys() performs the keyboard operations.
  • keyUp() releases the modifier key.

Clicking a Button Without Using click()

1. JavaScriptExecutor

 
JavascriptExecutor js =
(JavascriptExecutor) driver;

js.executeScript("arguments[0].click();", element);
 

2. sendKeys()

 
element.sendKeys(Keys.RETURN);
 

or

 
element.sendKeys(Keys.ENTER);
 

3. submit()

For submit buttons inside a form.

 
element.submit();
 

4. Actions Class

Move to the element and perform a click.

 
Actions actions = new Actions(driver);

actions.moveToElement(element)
       .click()
       .perform();
 

FAQs

1. What is the Actions class used for?

The Actions class is used to perform complex user interactions such as:

  • Mouse movements
  • Right-click
  • Drag and drop
  • Hover
  • Keyboard operations

It is created using:

 
Actions actions = new Actions(driver);
 

2. How do you perform a right-click?

Use the contextClick() method.

 
actions.contextClick(element).perform();
 

3. What is the difference between build() and perform()?

build()

  • Creates a composite action.
  • Used when multiple actions are chained.
  • Returns an Action object.

perform()

  • Executes the action(s).

Use:

  • perform() for a single action.
  • build().perform() for multiple chained actions.

4. How do you perform drag and drop?

 
actions.dragAndDrop(source, target)
       .build()
       .perform();
 

Or manually:

 
actions.clickAndHold(source)
       .moveToElement(target)
       .release()
       .build()
       .perform();
 

5. How do you hover over an element?

Use:

 
actions.moveToElement(element)
       .perform();
 

For multi-level menus, chain additional moveToElement() and click() methods.


6. How do you perform Ctrl + C in Selenium?

 
actions.keyDown(Keys.CONTROL)
       .sendKeys("a")
       .sendKeys("c")
       .keyUp(Keys.CONTROL)
       .perform();
 

7. How do you click a button without using click()?

You can use:

  • JavascriptExecutor
 
arguments[0].click();
 
  • sendKeys(Keys.ENTER)
  • submit() (for form buttons)
  • Actions class using moveToElement().click()