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
- Call
clickAndHold()with the box andperform(). - Pause for the time the task gives with
Thread.sleep(); the page needs about a second. - Print the box's text after the label
During hold:. - Release the mouse with
release()andperform().
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.
Related lessons
- Drag and Drop with the Actions ClassThe complete drag that
clickAndHold()starts. - Keyboard - Actions Class Key EventsHold a key down instead of a mouse button.
- Explicit Wait - Element VisibleWait for a change with
WebDriverWaitinstead of a fixed sleep.
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.
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();
}
}