Bird
Raised Fist0
Selenium Pythontesting~15 mins

Drag and drop in Selenium Python - Build an Automation Script

Choose your learning style10 modes available
Verify drag and drop functionality on demo page
Preconditions (2)
Step 1: Switch to the frame containing the draggable and droppable elements
Step 2: Locate the draggable element with id 'draggable'
Step 3: Locate the droppable element with id 'droppable'
Step 4: Drag the draggable element and drop it onto the droppable element
✅ Expected Result: The droppable element's text changes to 'Dropped!' indicating successful drag and drop
Automation Requirements - Selenium with Python
Assertions Needed:
Verify the text of the droppable element changes to 'Dropped!' after the drag and drop action
Best Practices:
Use explicit waits to ensure elements are present before interacting
Use Selenium's ActionChains for drag and drop
Switch to the correct iframe before interacting with elements inside it
Use meaningful locators like id or CSS selectors
Automated Solution
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains

# Initialize the Chrome driver
with webdriver.Chrome() as driver:
    driver.get('https://jqueryui.com/droppable/')

    wait = WebDriverWait(driver, 10)

    # Wait for the iframe to be present and switch to it
    iframe = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '.demo-frame')))
    driver.switch_to.frame(iframe)

    # Wait for draggable and droppable elements
    draggable = wait.until(EC.presence_of_element_located((By.ID, 'draggable')))
    droppable = wait.until(EC.presence_of_element_located((By.ID, 'droppable')))

    # Perform drag and drop
    actions = ActionChains(driver)
    actions.drag_and_drop(draggable, droppable).perform()

    # Verify the text changed to 'Dropped!'
    dropped_text = droppable.text
    assert dropped_text == 'Dropped!', f"Expected text 'Dropped!' but got '{dropped_text}'"

    # Switch back to default content
    driver.switch_to.default_content()

This script opens the demo page with drag and drop functionality.

It waits explicitly for the iframe to load, then switches into it because the draggable and droppable elements are inside an iframe.

It waits for the draggable and droppable elements to be present, then uses Selenium's ActionChains to drag the draggable element onto the droppable element.

After the action, it asserts that the droppable element's text changes to 'Dropped!' which confirms the drag and drop worked.

Finally, it switches back to the main page content.

Common Mistakes - 4 Pitfalls
Not switching to the iframe before interacting with elements inside it
Using time.sleep() instead of explicit waits
Using incorrect locators like absolute XPath for draggable and droppable elements
{'mistake': 'Not verifying the result after drag and drop', 'why_bad': 'Without assertions, the test does not confirm if the drag and drop actually succeeded.', 'correct_approach': "Assert that the droppable element's text changes to 'Dropped!' after the action."}
Bonus Challenge

Now add data-driven testing with 3 different draggable and droppable pairs on a similar page

Show Hint

Practice

(1/5)
1. What is the main purpose of using drag and drop in Selenium testing?
easy
A. To verify page title correctness
B. To check if a button is clickable
C. To input text into a form field
D. To simulate moving an element from one place to another on a webpage

Solution

  1. Step 1: Understand drag and drop action

    Drag and drop simulates the user moving an element by mouse from one location to another on the webpage.
  2. Step 2: Identify the purpose in Selenium

    Selenium uses drag and drop to test this user interaction, not for clicking buttons or inputting text.
  3. Final Answer:

    To simulate moving an element from one place to another on a webpage -> Option D
  4. Quick Check:

    Drag and drop = move element [OK]
Hint: Drag and drop means moving elements with mouse [OK]
Common Mistakes:
  • Confusing drag and drop with clicking
  • Thinking drag and drop inputs text
  • Assuming drag and drop checks page title
2. Which of the following is the correct way to perform drag and drop using Selenium's ActionChains in Python?
easy
A. actions.dragdrop(source, target).perform()
B. actions.dragdrop(source, target).execute()
C. actions.drag_and_drop(source, target).perform()
D. actions.drag_and_drop(source, target).execute()

Solution

  1. Step 1: Recall correct method name and usage

    The correct method in Selenium's ActionChains is drag_and_drop(source, target) followed by perform() to execute.
  2. Step 2: Check method chaining and spelling

    Options with dragdrop or execute() are incorrect because method names and chaining are wrong.
  3. Final Answer:

    actions.drag_and_drop(source, target).perform() -> Option C
  4. Quick Check:

    Correct method = drag_and_drop + perform() [OK]
Hint: Use drag_and_drop() then perform() to execute [OK]
Common Mistakes:
  • Using dragdrop instead of drag_and_drop
  • Calling execute() instead of perform()
  • Misspelling method names
3. Given the following code snippet, what will be the output if the drag and drop is successful?
from selenium.webdriver import ActionChains
actions = ActionChains(driver)
actions.drag_and_drop(source_element, target_element).perform()
result_text = driver.find_element(By.ID, 'result').text
print(result_text)

Assuming the page updates the text to 'Dropped!' after a successful drop.
medium
A. 'Dropped!'
B. 'Drag failed'
C. Empty string
D. Throws NoSuchElementException

Solution

  1. Step 1: Understand the code flow

    The code performs drag and drop, then reads text from element with ID 'result'.
  2. Step 2: Interpret expected page behavior

    If drag and drop is successful, the page updates 'result' text to 'Dropped!'. So print outputs 'Dropped!'.
  3. Final Answer:

    'Dropped!' -> Option A
  4. Quick Check:

    Successful drop updates text = 'Dropped!' [OK]
Hint: Check page text after drag to confirm success [OK]
Common Mistakes:
  • Assuming drag_and_drop returns a value
  • Expecting exceptions without checking element presence
  • Ignoring page text update after drop
4. You wrote this code to drag and drop but it fails silently (no error, no effect):
actions = ActionChains(driver)
actions.click_and_hold(source).move_to_element(target).release().perform()

What is the most likely reason for failure?
medium
A. The source or target elements are not interactable or visible
B. You forgot to import ActionChains
C. The perform() method is missing
D. The driver is not initialized

Solution

  1. Step 1: Analyze the drag and drop steps

    The code uses click_and_hold, move_to_element, release, and perform correctly.
  2. Step 2: Identify common silent failure cause

    If no error but no effect, often source or target elements are hidden, disabled, or not interactable.
  3. Final Answer:

    The source or target elements are not interactable or visible -> Option A
  4. Quick Check:

    Invisible elements cause silent drag failure [OK]
Hint: Check element visibility and interactability first [OK]
Common Mistakes:
  • Assuming missing perform() causes silent failure
  • Ignoring element visibility issues
  • Forgetting to import ActionChains causes errors, not silent failure
5. You want to test a drag and drop feature where the target area only accepts drops if the dragged element has a specific CSS class 'draggable'. Which approach correctly verifies this behavior in Selenium Python?
hard
A. Drag any element and check if the page URL changes
B. Drag the element with 'draggable' class and assert the target's text changes; drag element without class and assert no change
C. Only check if drag_and_drop() method runs without error
D. Use JavaScript to forcibly move the element without checking CSS classes

Solution

  1. Step 1: Understand the acceptance condition

    The target accepts drops only if the dragged element has 'draggable' class, so test both cases.
  2. Step 2: Verify behavior by asserting page changes

    Drag element with class and check target text changes; drag element without class and check no change to confirm correct behavior.
  3. Final Answer:

    Drag the element with 'draggable' class and assert the target's text changes; drag element without class and assert no change -> Option B
  4. Quick Check:

    Test both valid and invalid drag cases [OK]
Hint: Test drag with and without class, check target changes [OK]
Common Mistakes:
  • Ignoring negative test case without class
  • Assuming method success means feature works
  • Using JavaScript to bypass UI logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard