Click and hold in Selenium Java - Build an Automation Script
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; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import static org.junit.jupiter.api.Assertions.assertEquals; public class ClickAndHoldTest { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); WebDriver driver = new ChromeDriver(); try { driver.get("https://example.com/testpage"); WebElement button = driver.findElement(By.id("hold-button")); Actions actions = new Actions(driver); actions.clickAndHold(button).pause(Duration.ofSeconds(3)).release().perform(); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5)); wait.until(ExpectedConditions.textToBe(By.id("hold-button"), "Held!")); String buttonText = button.getText(); assertEquals("Held!", buttonText, "Button text should be 'Held!' after click and hold"); System.out.println("Test passed: Button text changed to 'Held!'"); } finally { driver.quit(); } } }
This test script uses Selenium WebDriver with Java to automate the click and hold action.
First, it opens the test page and locates the button by its id 'hold-button'.
Then, it uses the Actions class to perform a click and hold on the button for 3 seconds, then releases the click.
After that, it waits explicitly for the button text to change to 'Held!' using WebDriverWait and ExpectedConditions.textToBe.
Finally, it asserts that the button text is exactly 'Held!'.
The test prints a success message if the assertion passes and closes the browser in the finally block to ensure cleanup.
Now add data-driven testing with 3 different hold durations: 1 second, 3 seconds, and 5 seconds, verifying the button text changes accordingly.
Practice
clickAndHold() method in Selenium's Actions class do?Solution
Step 1: Understand the purpose of clickAndHold()
TheclickAndHold()method simulates pressing and holding the mouse button down on a specific web element without releasing it.Step 2: Compare with other mouse actions
Unlike a simple click or double click,clickAndHold()keeps the mouse button pressed, useful for drag-and-drop or selection actions.Final Answer:
Simulates pressing and holding the mouse button on a web element -> Option AQuick Check:
clickAndHold() = Press and hold mouse [OK]
- Confusing clickAndHold() with click()
- Thinking it releases the mouse button immediately
- Assuming it moves the mouse pointer only
element using Selenium in Java?Solution
Step 1: Use Actions class instance
In Selenium Java, you create an Actions object withnew Actions(driver)to perform complex mouse actions.Step 2: Call clickAndHold() on the element and then perform()
The correct syntax is chainingclickAndHold(element)followed byperform()to execute the action.Final Answer:
new Actions(driver).clickAndHold(element).perform(); -> Option DQuick Check:
Actions + clickAndHold + perform() = Correct syntax [OK]
- Calling clickAndHold() directly on driver
- Missing perform() at the end
- Using incorrect method chaining syntax
Actions actions = new Actions(driver);
WebElement box = driver.findElement(By.id("box"));
actions.clickAndHold(box).pause(Duration.ofSeconds(2)).release().perform();Solution
Step 1: Analyze the action chain
The code callsclickAndHold(box)to press the mouse button down on the element, then pauses for 2 seconds, then releases the mouse button.Step 2: Understand pause() and release()
Thepause(Duration.ofSeconds(2))waits 2 seconds while holding, andrelease()releases the mouse button, thenperform()executes all actions.Final Answer:
Clicks and holds on the element with id 'box' for 2 seconds, then releases the mouse button -> Option AQuick Check:
clickAndHold + pause + release = Hold 2s then release [OK]
- Thinking pause() is invalid in Actions chain
- Assuming immediate release after clickAndHold
- Confusing release() with click()
Actions actions = new Actions(driver);
WebElement element = driver.findElement(By.id("drag"));
new Actions(driver).clickAndHold(element);
actions.perform();Solution
Step 1: Check method chaining
TheclickAndHold(element)returns an Actions object and should be chained withperform()to execute immediately.Step 2: Analyze the code calls
Here,clickAndHold(element)is called but not chained; thenactions.perform()is called separately, which performs no action because the previous call was not chained.Final Answer:
Missing chaining of clickAndHold() with perform() -> Option BQuick Check:
Chain clickAndHold().perform() to run action [OK]
- Calling perform() separately without chaining
- Assuming perform() runs all previous actions
- Not understanding fluent interface chaining
source, move it to an element with id target, then release. Which of the following code snippets correctly performs this using Selenium Java?Solution
Step 1: Understand drag-and-drop with clickAndHold
To drag and drop manually, you click and hold the source element, move to the target element, then release the mouse button.Step 2: Analyze each option
new Actions(driver).clickAndHold(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).release().perform(); correctly chainsclickAndHold(source),moveToElement(target),release(), andperform(). new Actions(driver).click(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).click().perform(); uses click instead of clickAndHold, new Actions(driver).dragAndDrop(By.id("source"), By.id("target")).perform(); incorrectly passes By locators instead of WebElements, new Actions(driver).clickAndHold(By.id("source")).moveToElement(By.id("target")).release().perform(); incorrectly passes By locators instead of WebElements.Final Answer:
new Actions(driver).clickAndHold(driver.findElement(By.id("source"))).moveToElement(driver.findElement(By.id("target"))).release().perform(); -> Option CQuick Check:
clickAndHold + moveToElement + release = drag and drop [OK]
- Passing By locators instead of WebElements
- Using click instead of clickAndHold for drag
- Confusing dragAndDrop() with manual actions
Ready to prove it?
Pass the test and this pattern moves unknown
