0
0
Selenium-pythonHow-ToBeginner ยท 4 min read

How to Drag and Drop Using Selenium Python: Simple Guide

To perform drag and drop in Selenium Python, use the ActionChains class with its drag_and_drop(source, target) method. This simulates clicking and holding the source element, moving it to the target element, and releasing it.
๐Ÿ“

Syntax

The main syntax to drag and drop in Selenium Python uses the ActionChains class:

  • ActionChains(driver): Creates an action chain object.
  • drag_and_drop(source, target): Defines the drag and drop action from the source element to the target element.
  • perform(): Executes the defined actions.
python
from selenium.webdriver import ActionChains

actions = ActionChains(driver)
actions.drag_and_drop(source_element, target_element).perform()
๐Ÿ’ป

Example

This example demonstrates how to open a webpage with draggable and droppable elements, then drag one element and drop it onto another using Selenium Python.

python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver import ActionChains
import time

# Setup WebDriver (Chrome in this example)
driver = webdriver.Chrome()
driver.get('https://jqueryui.com/droppable/')

# Switch to the frame containing draggable elements
frame = driver.find_element(By.CSS_SELECTOR, '.demo-frame')
driver.switch_to.frame(frame)

# Locate source and target elements
source = driver.find_element(By.ID, 'draggable')
target = driver.find_element(By.ID, 'droppable')

# Create ActionChains object and perform drag and drop
actions = ActionChains(driver)
actions.drag_and_drop(source, target).perform()

# Wait to see the result
time.sleep(3)

driver.quit()
Output
The draggable box is moved and dropped onto the droppable box, which changes its text to 'Dropped!'.
โš ๏ธ

Common Pitfalls

Common mistakes when using drag and drop in Selenium Python include:

  • Not switching to the correct iframe if elements are inside one.
  • Using incorrect locators that do not find the source or target elements.
  • Forgetting to call perform() to execute the action chain.
  • Trying drag and drop on elements that do not support it or require JavaScript events.

Example of a wrong and right way:

python
# Wrong way: Missing perform()
actions = ActionChains(driver)
actions.drag_and_drop(source, target)  # No perform(), so no action happens

# Right way:
actions.drag_and_drop(source, target).perform()
๐Ÿ“Š

Quick Reference

MethodDescription
ActionChains(driver)Create an action chain object to perform complex user interactions
drag_and_drop(source, target)Define drag and drop from source element to target element
perform()Execute all actions defined in the chain
switch_to.frame(frame_element)Switch to iframe if elements are inside one
find_element(By, locator)Locate elements on the page
โœ…

Key Takeaways

Use ActionChains with drag_and_drop(source, target).perform() to drag and drop elements.
Always switch to the correct iframe if draggable elements are inside one.
Ensure locators correctly find the source and target elements before performing actions.
Never forget to call perform() to execute the drag and drop action.
Test on real pages to confirm drag and drop works as expected.