Critical Actions class in selenium -Absolute 2021 guide

We are going to discuss and learn exhaustive usage of Actions class in Selenium across different Web operations . Actions Class in Selenium is widely used to perform various web operations such as mouse and keyboard movement which are advance webbrowser actions.

Actions class in selenium
Actions class in Selenium

Mouse Actions

  • Drag and drop in selenium
  • ClickAndHold in Selenium
  • moveToElement in Selenium 
  • dragAndDropBy in Selenium 
  • MoveByOffset in Selenium
  • DoubleClick in Selenium
  • RightClick or ContextClick in selenium
  • Pause in Selenium
  • Mouse hover in selenium

Keyboard Actions

  • sendKeys in Selenium
  • KeyUp in Selenium
  • KeyDown in Selenium

Actions class in selenium

What is Actions Class in Selenium : 

Actions Class is a class from Selenium Bundle Api from the package org.openqa.selenium.interactions to handle Critical and advance Web interactions with Users through browser Automation. Provides users with a handle to perform various Mouse and Keyboard Actions via different methods mentioned above.  

Actions class in selenium
Actions class in Selenium

We will discuss here all the Web interactions via Actions Class in Selenium, to start with We learn the Mouse Interactions and move towards KeyBoard Interactions with Actions class in Selenium.

Mouse Interactions – Actions Class in Selenium

Drag and drop in selenium

Drag and Drop in Selenium can be done in three different approaches with Actions class in Selenium : 

  • With build() method 
  • With dragAndDropBy() method
  • With dragAndDrop() method.

Drag and Drop in Selenium with Action Class in Selenium via build method : 

We need to have two webelements to perform Drag and Drop in Selenium such as Source WebElement (which is to be dragged) and Destination WebElement (Where the Source WebElement to be dropped, i.e. the Destination WebElement ), The below method is a customized method (which you could use to build your Framework WebUtils) which perform the Drag And Drop in Selenium and this method takes two arguments one is Source WebElement, and another one is Destination WebElement as mentioned previously: 

public void dragAndDrop(By source, By destination) {
   try {
       WebElement sourceElement = driver.findElement(source);
       WebElement destinationElement = driver.findElement(destination);
       Actions builder = new Actions(driver);
       Action dragAndDrop = builder.clickAndHold(sourceElement).moveToElement(destinationElement)
               .release(destinationElement).build();
       dragAndDrop.perform();
       BASE_LOGGER.info("Successfully performed the Drag and Drop action ");
   } catch (Exception e) {
       String exceptionData = e.getCause().getMessage();
       BASE_LOGGER.error("Error encountered i.e : " + exceptionData + " while performing DragAndDrop ");
   }
}

To learn the whole concept about drag and drop and get to all the approaches to perform Drag and Drop in Selenium, click here.

ClickAndHold in Selenium 

ClickAndHold is another important method from the Actions class (from org.openqa.selenium.interactions package) in Selenium to
first perform left click operation over any webElement and hold it without releasing mouse.

This method is majorly used while performing the drag and drop scenarios, below is the sample code image : 

Actions class in selenium
click and hold -Actions class in Selenium

MoveToElement in Selenium

MoveToElement is a method from the Actions class in Selenium to perform the movement to another WebElement (Destination Webelement), which this method takes as one respective argument.

This method is majorly used while performing the drag and drop scenarios, below is the sample code image : 

Move to element – Actions class in Selenium

Double Click in Selenium

To replicate the double click operation via mouse we need to Perform double click via Actions class in Selenium and we can do it in the below approach : 

public void doubleClick(By locator) {
   try {
       WebElement element = driver.findElement(locator);
       Actions actions = new Actions(driver);
       actions.doubleClick(element).perform();
       BASE_LOGGER.info("Performed the double Click on the Element  : " + locator);
   } catch (StaleElementReferenceException e) {
       BASE_LOGGER.error("Element is not attached to the page document " + e.getCause().getMessage());
   } catch (NoSuchElementException e) {
       BASE_LOGGER.error("Element " + locator + " was not found in DOM " + e.getCause().getMessage());
   } catch (Exception e) {
       BASE_LOGGER.error("Element " + locator + " was not clickable " + e.getCause().getMessage());
   }
}

The above code snippet is a method which basically takes an argument as Locator, i.e. the WebElement on which the double click has to be performed. 

context click or Selenium right click

To replicate the context click or right click operation via mouse we need to Perform context click method via Actions class in Selenium and we can do it in the below approach : 

public void rightClick(By locator) {
   try {
       WebElement element = driver.findElement(locator);
       Actions actions = new Actions(driver);
       actions.contextClick(element).perform();
       BASE_LOGGER.info("Performed the context Click on the Element  : " + locator);
   } catch (StaleElementReferenceException e) {
       BASE_LOGGER.error("Element is not attached to the page document " + e.getCause().getMessage());
   } catch (NoSuchElementException e) {
       BASE_LOGGER.error("Element " + locator + " was not found in DOM " + e.getCause().getMessage());
   } catch (Exception e) {
       BASE_LOGGER.error("Element " + locator + " was not clickable " + e.getCause().getMessage());
   }
}

The above code snippet is a method which basically takes an argument as Locator, i.e. the WebElement on which the double click has to be performed. 

Pause in Selenium 

Whenever we want to have introduced any time delay between different Actions we could use the pause method in between the Actions operations, like let’s say if we want to have some few seconds of delay between drag and drop operation, then we can call the pause() method of Actions Class in Selenium in below approach : 

public void pauseBetweenActions(By source,By destination, int timeUnit) {
   try {
       WebElement sourceElement = driver.findElement(source);
       WebElement destinationElement = driver.findElement(destination);
       Actions builder = new Actions(driver);
       builder.clickAndHold(sourceElement).moveToElement(destinationElement).pause(timeUnit).release(destinationElement).build().perform();
       BASE_LOGGER.info("Successfully performed the Drag and Drop action ");
   } catch (Exception e) {
       String exceptionData = e.getCause().getMessage();
       BASE_LOGGER.error("Error encountered i.e : " + exceptionData + " while performing DragAndDrop ");
   }
}

In the above method it takes three arguments: one source WebElement, another argument is destination element, and the last one is for timeUnit, i.e. the time delay we want to introduce using the pause() in Selenium.

Mouse hover in selenium

Mouse Hover in Selenium operation can be achieved by using Actions class in Selenium, basically, once we are in focus with any webElement and from there, we want to hover your mouse cursor to any other element by this Mouse Hover operation, and this WebElement where we want to take our mouse cursor is called as Target WebElement. 

Below is the code snippet for the same : 

public void mouseHover(By targetedLocator) {
   try {
       WebElement targetedElement = driver.findElement(targetedLocator);
       Actions builder = new Actions(driver);
       builder.moveToElement(targetedElement).build().perform();
       BASE_LOGGER.info("Successfully performed the Mouse hover in Selenium action ");
   } catch (Exception e) {
       String exceptionData = e.getCause().getMessage();
       BASE_LOGGER.error("Error encountered i.e : " + exceptionData + " while performing Mouse hover in Selenium ");
   }
}

To learn about all the Selenium webdriver command you can refer here .

Keyboard Actions – Actions Class in Selenium

What is Keyboard actions and where its required :

Keyboard actions means where the operations are directly performed from a keyboard such as lets say we are trying to perform Contrl+A together or Control+C together or performing Key up or Keydown button from keyboard while interacting with WebElements .

We can perform several Keyboard interactions with the help of Actions class in Selenium

sendKeys in Selenium :

We can perform sendKeys operation with the help of Actions class in Selenium in the below manner :

public void sendKeysWithActionClass(By locator) {
        try {
            WebElement element = driver.findElement(locator);
            Actions actions = new Actions(driver);
            actions.sendKeys("KeyBoard Data Entry ");

            // Perform Keyboard Actions ex pressing Control and c together

            actions.sendKeys(Keys.CONTROL);
            actions.sendKeys("c");
            actions.build().perform();
        } catch (Exception e) {
            String exceptionData = e.getCause().getMessage();
           
        }
    }

KeyUp in Selenium :

Similarly we can perform KeyUp operations with the Actions class in Selenium in the below manner :

    public void keyUpWithActionClass(By locator) {
        try {
            WebElement element = driver.findElement(locator);
            Actions actions = new Actions(driver);
            actions.sendKeys("KeyBoard Data Entry ");

            // Perform Keyboard Actions ex pressing Control and c together with keyUp

            actions.keyUp(Keys.CONTROL);
            actions.sendKeys("c");
            actions.build().perform();
        } catch (Exception e) {
            String exceptionData = e.getCause().getMessage();
        }
    }

KeyDown in Selenium

We can perform the keyDown operation with the help of again Actions class in Selenium in the below approach :

public void keyDownWithActionClass(By locator) {

        try {
            WebElement element = driver.findElement(locator);
            Actions actions = new Actions(driver);
            actions.sendKeys("KeyBoard Data Entry ");

            // Perform Keyboard Actions ex pressing Control and V together with keyDown

            actions.keyDown(Keys.CONTROL);
            actions.sendKeys("v");
            actions.build().perform();
        } catch (Exception e) {
            String exceptionData = e.getCause().getMessage();

        }
    }

Conclusion: With these we are done here with the Actions class in Selenium ,In the upcoming tutorial JavascriptExecutor in Selenium in modular yet exhaustive manner . To learn Exhaustively the Selenium command you can refer here also to prepare for Critical Selenium interview questions you can refer here .

About Debarghya

Myself Debarghya Roy, I am an Engineering ARCHITECT working with fortune 5 company and an open source contributor, having around 12 years of experience/expertise in various Technology stack.
I have worked with various technologies such as Java,C#,Python,Groovy, UI Automation(Selenium), Mobile Automation(Appium), API/Backend Automation,Performance Engineering(JMeter, Locust), Security Automation(MobSF,OwAsp,Kali Linux, Astra,ZAP etc), RPA,Process Engineering Automation,Mainframe Automation,Back End Development with SpringBoot,Kafka,Redis,RabitMQ,ELK stack, GrayLog, Jenkins and also having experience in Cloud Technologies, DevOps etc.
I live in Bangalore ,India with my wife and have passion towards Blogging , music , playing guitar and my Philosophy of life is Education for All which gave the birth of LambdaGeeks. Lets connect over linked-in - https://www.linkedin.com/in/debarghya-roy/

Leave a Comment

Your email address will not be published. Required fields are marked *