How to Perform Click and Hold in Selenium WebDriver

Click and hold is an action in Selenium in which we do left-click on an element and hold it without releasing the left button of the mouse.

To perform this action in Selenium WebDriver, we will use clickAndHold() method of actions class. This method is generally useful in executing drag and drop operations.

Let’s take a scenario where we will click and hold at the current location action. Follow all the steps to complete the below scenario.

Scenario to Automate:

1. Launch the Firefox browser.
2. Open the URL “https://selenium08.blogspot.com/2020/01/click-and-hold.html”.
3. Locate the element by By.xpath.
4. Move the cursor to the position of title C.
5. Click and hold title C.
6. Move the cursor to any position.
[adinserter block=”5″]
Let’s create a test case where we will automate the following scenario.

Program code 1:

package clickAndHoldAction; 
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 ClickAndHold { 
public static void main(String[] args) 
{ 
// Create a firefox driver object to launch the Firefox browser.
   WebDriver driver = new FirefoxDriver(); 

// Maximize the browser. 
   driver.manage().window().maximize(); 

// Create a variable URL of string type to store the url. 
   String url = "https://selenium08.blogspot.com/2020/01/click-and-hold.html"; 
   driver.get(url); 

// Locate the element C by By.xpath. 
   WebElement titleC = driver.findElement(By.xpath("//li[text()= 'C']")); 

// Create an object of actions class and pass the reference of WebDriver as a parameter to its constructor. 
   Actions actions = new Actions(driver); 

// Move the cursor to the position of element C. 
   actions.moveToElement(titleC); 

// Call clickAndHold() method to perform click and hold operation. 
   actions.clickAndHold().perform(); 
  } 
}

The title movement will be similar to the below screenshot as shown below.

How to perform Click and Hold in Selenium

In this example program, first, we move cursor to the location of title C and then click and hold title C. When we will move the cursor to the location of title B then title B has been shifted to location of title C.

You can also move title C anywhere. Now execute this in your eclipse and see what happens.
[adinserter block=”2″]

How to Perform Click and Hold WebElement Action in Selenium WebDriver?


In the previous section, we have seen clickAndHold() method of actions class which will click and hold a WebElement at the current position of the cursor. But this method does not deal with a particular web element.

So, if you want to deal with a particular WebElement on a web page, you will have to use an overloaded version of clickAndHold() method of actions class. It helps to avoid moving of cursor to location of any web element.

The API syntax of an overloaded version of clickAndHold method is as follows:

public Actions clickAndHold(WebElement onElement)

This method accepts a WebElement as an input parameter that has to be clicked and held.

Let’s modify coding in the previous example to use an overloaded version of clickAndHold() method.

Program code 2:

package clickAndHoldAction; 
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 ClickAndHold { 
public static void main(String[] args) 
{ 
   WebDriver driver = new FirefoxDriver(); 
   driver.manage().window().maximize(); 
 
   String url = "https://selenium08.blogspot.com/2020/01/click-and-hold.html"; 
   driver.get(url); 

// Locate the element C by By.xpath. 
   WebElement titleC = driver.findElement(By.xpath("//li[text()= 'C']")); 

// Create an object of actions class and pass the reference of WebDriver as a parameter to its constructor. 
   Actions actions = new Actions(driver); 

// Call clickAndHold() method to perform click and hold operation on element C. 
   actions.clickAndHold(titleC).perform(); 
  } 
}

The only change in this coding is that we have removed the action of moving cursor and provided WebElement to clickAndHold() method that will take care of identifying of WebElement.


In this tutorial, we have covered almost all important points related to click and hold in Selenium WebDriver. Hope that you will have understood this topic and performed the scenario in your eclipse. In the next, we will learn how to move an element and click in Selenium.
Thanks for reading!!!

⇐ PrevNext ⇒