Drag and drop helps test if you can move items on a webpage by clicking and dragging them.
0
0
Drag and drop in Selenium Python
Introduction
Testing if a user can move a file icon into a folder on a webpage.
Checking if a shopping cart allows dragging products into it.
Verifying if a puzzle piece can be dragged to the correct spot.
Testing drag and drop in a form builder interface.
Ensuring a user can reorder list items by dragging.
Syntax
Selenium Python
from selenium.webdriver import ActionChains # Create action chain object actions = ActionChains(driver) # Drag source element and drop on target element actions.drag_and_drop(source_element, target_element).perform()
Use ActionChains to perform complex mouse actions like drag and drop.
Always call perform() to execute the action.
Examples
Drag the source element and drop it onto the target element in one step.
Selenium Python
actions.drag_and_drop(source, target).perform()
Manually click and hold the source, move to target, then release to drop.
Selenium Python
actions.click_and_hold(source).move_to_element(target).release().perform()
Sample Program
This script opens a test page with drag and drop, drags the box onto the drop area, waits 2 seconds, then prints the drop area's text to confirm success.
Selenium Python
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains import time # Setup driver (make sure chromedriver is in PATH) driver = webdriver.Chrome() driver.get('https://crossbrowsertesting.github.io/drag-and-drop.html') # Locate source and target elements source = driver.find_element(By.ID, 'draggable') target = driver.find_element(By.ID, 'droppable') # Create action chain and perform drag and drop actions = ActionChains(driver) actions.drag_and_drop(source, target).perform() # Wait to see result time.sleep(2) # Verify if drop was successful by checking text result_text = target.text print(result_text) driver.quit()
OutputSuccess
Important Notes
Drag and drop may fail if elements are inside iframes; switch to the iframe first.
Sometimes drag and drop needs small pauses between actions for reliability.
Use explicit waits to ensure elements are ready before dragging.
Summary
Drag and drop tests moving items by mouse on a webpage.
Use Selenium's ActionChains with drag_and_drop or click_and_hold methods.
Always verify the drop by checking page changes or element states.