clickAndHold() method in Selenium Java with Examples

clickAndHold() method in Selenium helps simulate clicking and to hold down the mouse button on an element. It’s part of the Actions class, which provides a way to perform complex user interactions with the web page. This post will discuss the clickAndHold() method of the Actions class in detail.

Actions class has two overloaded implementations of the clickAndHold() methods

Let’s look at each of the methods one by one. We will use our selenium playground website to click and hold one of the boxes on the page – https://testkru.com/Interactions/DragAndDrop.

Let’s pick Box 1 and will try to click and hold on to the box.

Highlighting Box 1 for click and hold
public Actions clickAndHold(WebElement target)

It accepts a WebElement parameter, and the clickAndHold() method will move to that element and click ( without releasing ) in the middle of the element. In other words, it clicks and holds the mouse button on the specified element.

Let’s now use the clickAndHold() method on the highlighted box in the above image.

First, we will have to find the element. So, let’s inspect the highlighted box.

Box 1 element

We can see that the id attribute value is “box1“. We can use this to find the element using the findElement() method.

WebElement webElement = driver.findElement(By.id("box1"));

And then, we can use the click and hold the element using the code below.

Actions actions = new Actions(driver);
actions.clickAndHold(webElement).build().perform();
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 CodekruTest {

	public static void main(String[] args) {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		driver.get("https://testkru.com/Interactions/DragAndDrop");

		WebElement webElement = driver.findElement(By.id("box1"));

		Actions actions = new Actions(driver);
		actions.clickAndHold(webElement).build().perform();

	}

}

Above code will click and then hold the specified element. If we hold the box for a minute, the text will change from “Box 1” to “Holding box 1“.

Click and holding the box
public Actions clickAndHold()

This function will click and hold the mouse button on the current cursor position. This function is normally used with the moveToElement() method.

actions.moveToElement(webElement).clickAndHold().build().perform();

In the above code, we are first moving to an element and then clicking on the element without releasing the mouse button.

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 CodekruTest {

	public static void main(String[] args) {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		driver.get("https://testkru.com/Interactions/DragAndDrop");

		WebElement webElement = driver.findElement(By.id("box1"));

		Actions actions = new Actions(driver);
		actions.moveToElement(webElement).clickAndHold().build().perform();

	}

}

Note: clickAndHold(webElement) is equivalent to moveToElement(webElement).clickAndHold().

What if we pass null in the clickAndHold() method?

Here, we will get an IllegalArgumentException, as illustrated below.

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 CodekruTest {

	public static void main(String[] args) {

		// pass the path of the chromedriver location in the second argument
		System.setProperty("webdriver.chrome.driver", "E:\\chromedriver.exe");
		WebDriver driver = new ChromeDriver();

		driver.get("https://testkru.com/Interactions/DragAndDrop");

		WebElement webElement = driver.findElement(By.id("box1"));

		Actions actions = new Actions(driver);
		actions.clickAndHold(null).build().perform();

	}

}

Output –

Exception in thread "main" java.lang.IllegalArgumentException: Element must be set
	at org.openqa.selenium.internal.Require.nonNull(Require.java:59)
	at org.openqa.selenium.interactions.PointerInput$Origin.fromElement(PointerInput.java:287)
	at org.openqa.selenium.interactions.Actions.moveInTicks(Actions.java:342)
	at org.openqa.selenium.interactions.Actions.clickAndHold(Actions.java:188)

We hope that you have liked the article. If you have any doubts or concerns, please write to us in the comments or mail us at admin@codekru.com.

Liked the article? Share this on

Leave a Comment

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