About this lesson
Selenium Python drag_and_drop: move a box into a drop zone with ActionChains
Selenium Python AssignmentsAdvancedMouse and Keyboard
How it works
ActionChains.drag_and_drop(source, target) presses on the source, moves to the target and releases. Both arguments are elements, in that order. Passing only one raises TypeError for a missing positional argument target. perform() returns None, so success shows only in the page.
To check it, find the box again and walk up to its parent. The box starts inside one container, and after a good drop its parent is a different element. Reading .id on that parent gives the driver's internal reference, a long random string. Ask get_attribute for the HTML id instead.
Window size decides the outcome here. At the default 780 by 580, the drop zone sits below the visible area and the box stayed in its starting container. At 1280 by 900, set before loading the page, it landed in all five test runs. Setting the same size after the page had loaded was unreliable, with results changing from run to run, so resize first, then load the page and drag.
These failures are quiet. No exception is thrown when a drop is ignored, so the page state is your only evidence.
About the assignment
Drag the first box into the first drop zone and prove it moved by reading the id of the box's parent element.
What you will do
- Make the browser window 1280 wide and 900 tall, then open the drag and drop page.
- Find the box and the drop zone by their ids.
- Run a chain that drags the box onto the zone.
- Go from the box to its parent with an XPath step, read the parent's id through the attribute method, and print it after the label.
Common mistakes
Keeping the default window
At 780 by 580 the drop is ignored. The script finishes without an error, but the parent still has the starting container's id.
Reading .id for the parent
The
.idproperty is Selenium's own element reference, not the id written in the HTML.Passing only the box
Calling
drag_and_dropwith one element raisesTypeError: ActionChains.drag_and_drop() missing 1 required positional argument: 'target'.
In a real test suite
Drag is one of the flakiest gestures in UI testing. Teams fix the viewport in a fixture and check the result through page state, as here. Some apps need a manual press, move and release sequence instead.
The next lesson looks at a building block of drag: pressing and holding without moving.
Related lessons
- Set the Window SizeSets the window size, which this lesson needs before the drag works.
- Move to a Parent with XPathMoves from an element to its parent with XPath, used here to check the result.
- Click and Hold a BoxHolds the mouse button down on another box on the same page.
To try one of these calls on any page without a task, open the Online Selenium Python Compiler.
Starter code
The editor above opens with this code. Fill in the TODO lines, then run and submit it.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
try:
# Set the window to 1280 by 900, open https://testkru.com/Interactions/DragAndDrop, and drag #box1 onto #dropZone1
pass
finally:
driver.quit()