Click and hold lets you press and keep holding a mouse button on a webpage element. This helps test actions like dragging or revealing hidden menus.
0
0
Click and hold in Selenium Python
Introduction
Testing drag-and-drop features on a webpage.
Checking if holding a button shows a tooltip or menu.
Simulating long press actions on buttons or images.
Verifying interactive elements that respond to click and hold.
Syntax
Selenium Python
from selenium.webdriver.common.action_chains import ActionChains actions = ActionChains(driver) actions.click_and_hold(element).perform()
You need to import ActionChains from Selenium to use click and hold.
Always call perform() to execute the action.
Examples
Click and hold on the given element immediately.
Selenium Python
actions.click_and_hold(element).perform()
Click and hold the element for 2 seconds, then release the mouse button.
Selenium Python
actions.click_and_hold(element).pause(2).release().perform()Move mouse to element first, then click and hold.
Selenium Python
actions.move_to_element(element).click_and_hold().perform()
Sample Program
This script opens a demo page with a draggable box. It clicks and holds the box for 3 seconds, then releases it. Finally, it prints a success message and closes the browser.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains import time # Setup driver (make sure driver executable is in PATH) driver = webdriver.Chrome() driver.get('https://crossbrowsertesting.github.io/drag-and-drop.html') # Locate draggable element drag_element = driver.find_element(By.ID, 'draggable') # Create ActionChains object actions = ActionChains(driver) # Click and hold the draggable element for 3 seconds actions.click_and_hold(drag_element).pause(3).release().perform() print('Click and hold action performed successfully.') driver.quit()
OutputSuccess
Important Notes
Make sure the element is visible and interactable before clicking and holding.
Use pause(seconds) to hold the click for a specific time.
Always release the mouse button after holding to avoid stuck states.
Summary
Click and hold simulates pressing and holding the mouse button on an element.
Use it to test drag-and-drop or long press behaviors.
Remember to call perform() to run the action.