Drag and drop helps test if you can move items on a webpage by clicking and dragging them.
Drag and drop in 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.
actions.drag_and_drop(source, target).perform()
actions.click_and_hold(source).move_to_element(target).release().perform()
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.
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()
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.
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.
Practice
Solution
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.Step 2: Identify the purpose in Selenium
Selenium uses drag and drop to test this user interaction, not for clicking buttons or inputting text.Final Answer:
To simulate moving an element from one place to another on a webpage -> Option DQuick Check:
Drag and drop = move element [OK]
- Confusing drag and drop with clicking
- Thinking drag and drop inputs text
- Assuming drag and drop checks page title
Solution
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.Step 2: Check method chaining and spelling
Options with dragdrop or execute() are incorrect because method names and chaining are wrong.Final Answer:
actions.drag_and_drop(source, target).perform() -> Option CQuick Check:
Correct method = drag_and_drop + perform() [OK]
- Using dragdrop instead of drag_and_drop
- Calling execute() instead of perform()
- Misspelling method names
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.
Solution
Step 1: Understand the code flow
The code performs drag and drop, then reads text from element with ID 'result'.Step 2: Interpret expected page behavior
If drag and drop is successful, the page updates 'result' text to 'Dropped!'. So print outputs 'Dropped!'.Final Answer:
'Dropped!' -> Option AQuick Check:
Successful drop updates text = 'Dropped!' [OK]
- Assuming drag_and_drop returns a value
- Expecting exceptions without checking element presence
- Ignoring page text update after drop
actions = ActionChains(driver) actions.click_and_hold(source).move_to_element(target).release().perform()
What is the most likely reason for failure?
Solution
Step 1: Analyze the drag and drop steps
The code uses click_and_hold, move_to_element, release, and perform correctly.Step 2: Identify common silent failure cause
If no error but no effect, often source or target elements are hidden, disabled, or not interactable.Final Answer:
The source or target elements are not interactable or visible -> Option AQuick Check:
Invisible elements cause silent drag failure [OK]
- Assuming missing perform() causes silent failure
- Ignoring element visibility issues
- Forgetting to import ActionChains causes errors, not silent failure
Solution
Step 1: Understand the acceptance condition
The target accepts drops only if the dragged element has 'draggable' class, so test both cases.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.Final Answer:
Drag the element with 'draggable' class and assert the target's text changes; drag element without class and assert no change -> Option BQuick Check:
Test both valid and invalid drag cases [OK]
- Ignoring negative test case without class
- Assuming method success means feature works
- Using JavaScript to bypass UI logic
Ready to prove it?
Pass the test and this pattern moves unknown
