Actions Class in Selenium Webdriver

Actions Class in Selenium Webdriver: From our previous tutorials, if you have checked, we have covered various topics like WebDriver Element Methods to click a button using the click() and submit() methods. Also, we have discussed in our earlier articles how to handle dropdown values using Selenium.

After discussing the above topics, we are moving towards another important concept of Java Selenium WebDriver: the Action class. Action class can handle various types of Keyword and mouse operations. So, in this post, we will learn about the Selenium Action class and how we can interact with web applications.

What is the Actions class in Selenium?

Selenium Action class is an API that helps us perform complex user web interactions like double-click, right-click, drag-and-drop operations, etc. You can get a huge number of methods that we can use for performing various operations.

What Is an Action Interface?

In Selenium WebDriver, you can get Actions class and Action Interface. When you want to perform a single operation, then at that time, you can use the Action interface, and when your requirement is a series of actions, you can use the Actions class.

How to Use Actions class in Selenium?

Let us understand how to use the Action class With Selenium WebDriver.

  • Import Packages
  • Create Actions class Object
  • Generate The Actions Sequence
  • Build The Actions sequence.
  • Perform The Actions sequence.

Now, let us go through each step and try to understand what we have to do in each step to use the actions class in our automation script successfully.

Import Packages:

We all know that to implement something in our automation script, we must import the packages for that; only then can we use them in our script. Action and Actions classes are defined inside the below packages, so we must import them into our script.

import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Action;

Create Actions class Object.

As we have mentioned in the image, so many methods are present inside the Actions class. So, to use those methods, we need first to create an object of the Actions class, and with the help of that object, we can call those methods to perform different operations. So, to instantiate the Actions class, we need the WebDriver object. Here is the syntax below for the Instantiate Actions class:

Actions action = new Actions(webdriver object);

Generate actions sequence:

To achieve our requirement, we are performing a series of actions. For example, suppose you want to close a browser, so you have to press CTRL + F4 on the keyboard for that. Hence, for doing such operations, the Actions class provides some methods like keyDown,sendKeys, keyUp, etc.

If you have to think about CTRL + F4 for close, you Need t

  • Press CTRL Key
  • Press F4 key
  • Release CTRL key

So for that, if you have created a Sequence, then that should look something like below

  • actions.keyDown(Keys.ALT);
  • actions.sendKeys(Keys.F4);
  • actions.keyUp(Keys.ALT);

Build the Actions Sequence

In the above step, we have created a sequence of actions. Now, we have built the sequence using the build() method of actions class. This build() method will generate composite actions by containing all the actions that we have generated previously.

Action action = actions.build();

Perform actions sequence

The final step is to perform the action, which you can do by calling the perform() method of the Action interface. After executing this statement, you can see the action in the browser.

action.perform();

Actions class Methods of Selenium

In the Actions class, we can get various methods, but according to the behavior, it is categorized into 2 types:

  • Keyboard Events
  • Mouse Events

Keyboard Events Methods

  • sendKeys(keystone): sends a series of keystrokes onto the element
  • keyDown(theKey): Sends a keypress without releasing it. Subsequent actions may assume it as pressed. (example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL)
  • keyUp(theKey): Performs a key release

Mouse events Methods

  • click (): click on the element
  • doubleClick (): Double clicks on the element
  • contextClick(): Performs a context-click (right-click) on an element
  • clickAndHold(): Clicks at the present mouse location (without releasing)
  • dragAndDrop(source, target): Invokes click-and-hold at the source location and moves to the location of the target element before releasing the mouse. source – element to grab, target – element to release
  • dragAndDropBy(source, xOffset, yOffset): Performs click-and-hold at the source location, shifts by a given offset, and frees the mouse. xOffset – to shift horizontally, yOffset – to shift vertically
  • moveByOffset(x-offset, y-offset): Shifts the mouse from its current position (or 0,0) by the given offset. x-offset – Sets the horizontal offset (negative value – shifting the mouse to the left), y-offset – Sets the vertical offset (negative value – shifting the mouse to the up)
  • moveToElement(toElement): It shifts the mouse to the center of the element
  • release(): Releases the depressed left mouse button at the existing mouse location

In our Next Articles, we are going to discuss other operations that we can perform through the Actions class:

  • How to handle mouse hover actions using Actions in Selenium
  • How to do Drag and Drop using Actions in Selenium
  • How to Scroll Web Page Down or Up Using Selenium
  • How to Perform Context Click / Right Click using Actions in Selenium
  • How to Perform Double Click using Actions in Selenium

How do you click an element using an action class in the Java Selenium Program?

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Action_Click 
{
   WebDriver driver=new FirefoxDriver();
   Actions act=new Actions(driver);
   @BeforeTest
   public void openwebsite()
   {
      driver.manage().window().maximize();
      driver.get("https://www.google.co.in/");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   }
   @Test
   public void operation() throws InterruptedException
   {
      WebElement Element=driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a"));
      Actions act=new Actions(driver);
      act.moveToElement(Element).contextClick().build().perform();
      Thread.sleep(5000);
      act.sendKeys(Keys.DOWN,Keys.ENTER).build().perform();
      //act.click().build().perform();	
   }
   @AfterTest
   public void close() throws InterruptedException
   {
      Thread.sleep(5000);
      driver.close();
   }
}

How to Do Double Click Using Action Class In Java Selenium Program?

We may need to double-click on some elements when automating a web application. So, in this post, we will learn how to double-click an element with the help of an action class with a simple example.

In our previous post regarding action, the class uses selenium; we mentioned that with the help of the Action class, we can handle the keyboard and mouse actions.

Scenario to be automated:

In this example, we are going to automate the below scenario:

1. Open the Google.com in a browser
2. Find the Gmail Link on the Webpage and do the double click on the element
3. Close the browser

Automation Script Of the Above Scenario:

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Action_ContextClick 
{
   WebDriver driver=new FirefoxDriver();
   Actions act=new Actions(driver);
   @BeforeTest
   public void openwebsite()
   {
      driver.manage().window().maximize();
      driver.get("https://www.google.co.in/");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   }
   @Test
   public void operation() throws InterruptedException
   {
      WebElement Element=driver.findElement(By.xpath(".//*[@id='gbw']/div/div/div[1]/div[1]/a"));
      act=new Actions(driver);
      act.moveToElement(Element).build().perform();
      Thread.sleep(5000);
      act.click().build().perform();
      
   }
   @AfterTest
   public void close() throws InterruptedException
   {
      Thread.sleep(5000);
      driver.close();
   }
}

If you are still struggling to understand the above program, you can comment to us in the comment section or use our Contact Us page, where you can drop your concern, and we will try to clear up your confusion as soon as possible.

How to Move Specific Elements Using Action Class in Java Selenium Program?

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Actions_Dummy 
{
   public static void main(String[] args) throws Exception 
   {
      WebDriver driver=new FirefoxDriver();
      driver.manage().window().maximize();
      driver.get("http://flipkart.com/");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
      WebElement we=driver.findElement(By.xpath(".//*[@id='container']/div/header/div[2]/div/ul/li[3]/a/span"));
      Actions act=new Actions(driver);
      act.moveToElement(we).build().perform();	
   }
}

How does a mouse operate using an action class in a Java Selenium program?

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Action_MouseMove 
{
   /* Some Time You Got WebDriverException: Permission Denied When Ur Browser is not Compatible*/
   WebDriver driver=new FirefoxDriver();
   Actions act=new Actions(driver);
   @BeforeTest
   public void openwebsite()
   {
      driver.manage().window().maximize();
      driver.get("http://apps.qaplanet.in/qahrm/login.php");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   }
   @Test
   public void operation() throws InterruptedException
   {
      driver.findElement(By.name("txtUserName")).clear();
      driver.findElement(By.name("txtUserName")).sendKeys("qaplanet1");
      driver.findElement(By.name("txtPassword")).clear();
      driver.findElement(By.name("txtPassword")).sendKeys("user1");
      driver.findElement(By.name("Submit")).click();
      WebElement Element=driver.findElement(By.xpath(".//*[@id='leave']/a/span"));
      Actions act=new Actions(driver);
      act.moveToElement(Element).build().perform();;
      WebElement Element2=driver.findElement(By.xpath(".//*[@id='leave']/ul/li[3]/a/span"));
      act.moveToElement(Element2).build().perform();	
   }
   @AfterTest
   public void close() throws InterruptedException
   {
      Thread.sleep(5000);
      driver.close();
   }
}

How do you press the tab key using the action class in the Java Selenium example program?

package com.selenium.actions;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class Action_Keyboard 
{
   WebDriver driver=new FirefoxDriver();
   Actions act=new Actions(driver);
   @BeforeTest
   public void openwebsite()
   {
      driver.manage().window().maximize();
      driver.get("http://www.google.com");
      driver.manage().timeouts().implicitlyWait(15000, TimeUnit.SECONDS);
   }
   @Test
   public void operation() throws InterruptedException
   {
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys(Keys.TAB).build().perform();
      //act.sendKeys(Keys.TAB).build().perform();
      act.sendKeys("www.indjobupdate.in").build().perform();
      Thread.sleep(3000);
   }
   @AfterTest
   public void close() throws InterruptedException
   {
      Thread.sleep(5000);
      driver.close();
   }
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment