Skip to content
CodeKruPro

Click and Hold Action

55/70

Click and Hold

clickAndHold() presses the mouse button without releasing. Combined with moveToElement() and release(), it allows manual drag operations.

On the Drag and Drop page, holding a box for 1 second changes its text (e.g., "Holding Box 1").

Task:

  1. Navigate to https://testkru.com/Interactions/DragAndDrop
  2. Find Box 2 (id="box2")
  3. Use clickAndHold, wait 1.5 seconds, then release
  4. Print the box text during hold (it changes to "Holding Box 2")

Expected output:

During hold: Holding Box 2

About this lesson

clickAndHold in Selenium Java: press and hold the mouse with Actions

Learn Selenium JavaAdvancedActions Class

How it works

actions.clickAndHold(element).perform() presses the left mouse button on the element and leaves it down. release() lets it go. Between the two you can move the pointer, which is how manual drags are built: clickAndHold(source).moveToElement(target).release().

The boxes on the Drag and Drop page respond to holding. About a second after the button goes down, the box's label changes to say it is being held, and it changes back when the button is released over the box. Read straight after perform() and the label has not changed yet; half a second later it still has not.

The mouse stays pressed across commands. Until you call release(), later actions in the session still see the left button as down, so always release, even when the check in between fails.

About the assignment

The starter gives you an Actions object and box 2. You press and hold on the box, wait long enough for its label to change, read the label while holding, then release.

What you will do

  1. Call clickAndHold() with the box and perform().
  2. Pause for the time the task gives with Thread.sleep(); the page needs about a second.
  3. Print the box's text after the label During hold:.
  4. Release the mouse with release() and perform().

Common mistakes

  • Reading the text too early

    Without the pause, the box still shows its normal label, because the page waits about a second before changing it.

  • Reading after release

    Releasing restores the normal label, so the print must come between the hold and the release.

  • Never releasing

    The button stays down for the rest of the session: later pointer moves are reported with the left button pressed, and the box never gets its mouseup, so it keeps showing its held label.

In a real test suite

Press-and-hold drives long-press menus, "hold to confirm" buttons and custom drags on canvases and sliders. A pause inside the chain, pause(Duration.ofMillis(1500)), is a tidy alternative to Thread.sleep() when the hold is part of a single gesture.

Lesson 54 did a whole drag in one call. Lesson 56 moves from the mouse to the keyboard with special keys and key combinations.

To try one of these calls on any page without a task, open the Online Selenium Java Compiler.

Starter code

The editor above opens with this code. Fill in the TODO lines, then run and submit it.

Main.java
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Main {
    public static void main(String[] args) throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.get("https://testkru.com/Interactions/DragAndDrop");
        
        Actions actions = new Actions(driver);
        WebElement box = driver.findElement(By.id("box2"));
        
        // TODO: Click and hold the box
        // TODO: Wait 1.5 seconds for the text to change
        // TODO: Print the box text
        // TODO: Release the mouse
        
        driver.quit();
    }
}