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. So, let's take a scenario where we will click and hold at 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.

Let's see the following source code to automate the scenario.
Program source 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) { 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 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 in below.
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.

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 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:
Syntax:
       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 overloaded version of clickAndHold() method.
Program source 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 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.

Final words
Hope that this tutorial has covered almost all important points related to click and hold in Selenium WebDriver. I hope that you will have understood this topic and performed scenario in your eclipse.