Testing With Selenium WebDriver (80 Blogs) Become a Certified Professional
AWS Global Infrastructure

Software Testing

Topics Covered
  • Testing With Selenium WebDriver (70 Blogs)
SEE MORE

How to handle Actions class in Selenium WebDriver?

Last updated on Sep 10,2024 64K Views

5 / 14 Blog from Selenium Webdriver

Actions play a vital role in testing an application using Selenium. In order to perform any operations on the web application like accessing the drop-down box, double click, right click and many more, actions class is a necessity. On that note, I’m writing this article on how to handle the Actions class in Selenium to help you understand how actions work.

So, let’s get started.

What is the Actions class?

Actions class is a built-in ability to handle various types of keyboard and mouse events. In Selenium Webdriver, handling these events including operations such as drag and drop or clicking on multiple elements with the help of the control key are done using the advanced user interactions API. It mainly consists of Actions which are needed while performing various operations.

Actions-Class-in-Selenium - Edureka

This Actions class is the user-facing API for emulating complex action events. You can directly use this class rather than using the input devices, i.e. keyboard and mouse.


Actions action = new Actions(driver);
action.moveToElement(element).click().perform();

I will use perform() to execute the actions.

Using this Actions API, keyboard interactions can be easily handled by the  WebDriver. To use mouse actions, I will use the current location of the web element and then perform some kind of operation.

Now, let us learn about the different methods under this class that are used to perform some actions.

Different methods under Actions class

There are basically two methods which help in working with the actions in Selenium, namely:

  • Keyboard interface
  • Mouse interface

Let us learn about them in detail.

Keyboard interface

  • sendKeys(keysToSend): Sends a series of keystrokes onto the element.
  • keyDown(theKey): Sends a key press without release it. Subsequent actions may assume it as pressed.

Example: Keys.ALT, Keys.SHIFT, or Keys.CONTROL);

  • keyUp(theKey): Performs a key release.

Mouse interface

  • click(): Clicks on the element.
  • doubleClick (): Double clicks on the element.
  • contextClick() : Performs a context-click (right-click) on the element.
  • clickAndHold(): Clicks at the present mouse location without releasing.
  • dragAndDrop(source, target): Clicks 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 value, then frees the mouse. (X offset – to shift horizontally, Y Offset – 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 pressed left mouse button at the existing mouse location.

Once you get a proper idea about the different methods under the Actions class, it will be easy to implement a use case of the same. You can even check out the details of the testing methodologies tool with the Software testing course online.

How to handle actions class in Selenium

To handle these actions class in Selenium, you must follow a proper format.

  • Create an object of the Actions class ‘action‘ 
  • Focus on the element using WebDriver: action.moveToElement(element).build().perform();
  • Build().perform() is used to compile and execute the actions class.
  • Use the different methods under the actions class to perform various operations like click(), drag and drop and so on.

Related Learning: Drag and Drop in Selenium

Now let’s consider working on a demo to understand how Actions class work.

Demo

I will be performing actions on two websites namely edureka.co and jqueryui.com.

  • Download the Java libraries on to your system.
  • The first thing to do when you want to work with Selenium is to make sure you know how to install it to your system.
  • An IDE to work on. I prefer working on the Eclipse IDE because it is user-friendly and it is easy to execute Java projects.
  • Select the browser driver of your choice.
  • Get the URL of the web page that you want to test.
  • Perform corresponding actions like clicking the button, mouse move, key press and so on.

Refer this code to understand how actions are helpful while testing an application.


package seleniumdemo;

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.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;

public class Selenium {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.edureka.co/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Thread.sleep(3000);
Actions builder = new Actions(driver);
builder.moveToElement(driver.findElement(By.id("header_topcat"))).build().perform();
Thread.sleep(3000);
WebElement link = driver.findElement(By.cssSelector("#software-testing-certification-courses"));
builder.moveToElement(link).build().perform();
Thread.sleep(2000);
driver.findElement(By.xpath("//ul[@class='dropdown-menu']//li//a[text()='Software Testing']")).click();
Thread.sleep(4000);
WebElement act = driver.findElement(By.id("search-inp"));
builder.moveToElement(act).build().perform();
Thread.sleep(3000);
WebElement search = driver.findElement(By.xpath("//span[@class='typeahead__button']"));
builder.moveToElement(search).build().perform();
Thread.sleep(3000);
Action seriesOfActions;
seriesOfActions = builder
.sendKeys(act, "Selenium")
.keyDown(search, Keys.SHIFT)
.keyUp(search, Keys.SHIFT)
.build();
seriesOfActions.perform();
Thread.sleep(3000);
driver.quit();
}

}

The output for the above code is depicted below:

Searches for the drop down and clicks on it and then perform actions on the drop down.

Dropdown - Actions class in Selenium - Edureka

Clicks on “Software testing” and navigates to that page.

MouseClick - Actions class in Selenium - Edureka

Performs keyboard functions like KeyUp, KeyDown, and sendKeys().

Keyboard functions - Actions class in Selenium - Edureka

To perform drag and drop, I have considered this website which solely deals with drag and drop, jqueryui.com.


package seleniumdemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class demo {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:UsersVaishnaviDesktopchromedriver_win32 (2)chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://jqueryui.com/droppable/");
driver.manage().window().maximize();
driver.switchTo().frame(0);
WebElement SourceElement= driver.findElement(By.id("draggable"));
WebElement TargetElement= driver.findElement(By.id("droppable"));
Actions action = new Actions(driver);
Thread.sleep(3000);
action.dragAndDrop(SourceElement, TargetElement).build().perform();
//action.clickAndHold(SourceElement).moveToElement(TargetElement).release().build().perform();
Thread.sleep(3000);
driver.quit();

}

}

The output for the above code is depicted below:

 

Drag - Actions class in Selenium - Edureka

Drags the corresponding element to the target and drops it.

Drop - Actions class in Selenium - Edureka

With this, we come to an end to this “Actions class in Selenium” blog. I Hope you guys enjoyed this article and understood what are the actions and what are the different methods under these actions class in Selenium. Now that you have understood how to perform actions in Selenium, check out the Selenium Certification Course by Edureka, a trusted online learning company with a network of more than 650,000 satisfied learners spread across the globe. This course is designed to introduce you to the complete Selenium features and its importance in testing software.

For details, You can even check out test automation strategies and methodology concepts with the Automation testing training.

Got a question for us? Please mention it in the comments section of “Actions class in Selenium” and we will get back to you.

Upcoming Batches For Selenium Course
Course NameDateDetails
Selenium Course

Class Starts on 19th October,2024

19th October

SAT&SUN (Weekend Batch)
View Details
Selenium Course

Class Starts on 28th October,2024

28th October

MON-FRI (Weekday Batch)
View Details
Selenium Course

Class Starts on 25th November,2024

25th November

MON-FRI (Weekday Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

How to handle Actions class in Selenium WebDriver?

edureka.co