Test Overview
This test demonstrates why complex user interactions, like drag and drop, require Selenium's Actions class. It verifies that dragging an element moves it to the target location.
This test demonstrates why complex user interactions, like drag and drop, require Selenium's Actions class. It verifies that dragging an element moves it to the target location.
from selenium import webdriver from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC import unittest class TestDragAndDrop(unittest.TestCase): def setUp(self): self.driver = webdriver.Chrome() self.driver.get('https://jqueryui.com/droppable/') self.wait = WebDriverWait(self.driver, 10) def test_drag_and_drop(self): # Switch to demo frame self.wait.until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '.demo-frame'))) draggable = self.wait.until(EC.presence_of_element_located((By.ID, 'draggable'))) droppable = self.wait.until(EC.presence_of_element_located((By.ID, 'droppable'))) actions = ActionChains(self.driver) actions.drag_and_drop(draggable, droppable).perform() # Verify the drop was successful by checking text change drop_text = droppable.text self.assertEqual(drop_text, 'Dropped!') def tearDown(self): self.driver.quit() if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and Chrome browser opens | Browser window opens with empty tab | — | PASS |
| 2 | Navigates to 'https://jqueryui.com/droppable/' | Page loads showing draggable and droppable demo inside an iframe | Page loaded with demo frame present | PASS |
| 3 | Waits for iframe and switches to it | Inside iframe containing draggable and droppable elements | Iframe switched successfully | PASS |
| 4 | Finds draggable element by ID 'draggable' | Draggable element is visible and ready | Draggable element located | PASS |
| 5 | Finds droppable element by ID 'droppable' | Droppable element is visible and ready | Droppable element located | PASS |
| 6 | Uses ActionChains to drag draggable element and drop it onto droppable element | Drag and drop action performed by Selenium | — | PASS |
| 7 | Checks text of droppable element to verify drop success | Droppable element text changed to 'Dropped!' | Assert droppable.text == 'Dropped!' | PASS |
| 8 | Test ends and browser closes | Browser window closed | — | PASS |
Actions in Selenium for some interactions?Actions classActions lets you build these multi-step user interactions and then perform them together.ActionChains and it requires the driver as a parameter.ActionChains(driver) is the correct syntax to create an action chain object.from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
actions = ActionChains(driver)
element = driver.find_element(By.ID, 'btn')
actions.move_to_element(element).click().perform()
print('Action performed')from selenium.webdriver import ActionChains from selenium.webdriver.common.by import By actions = ActionChains(driver) drag = driver.find_element(By.ID, 'drag') drop = driver.find_element(By.ID, 'drop') actions.drag_and_drop(drag).perform()
key_down(Keys.CONTROL), then send_keys('a'), then key_up(Keys.CONTROL), and finally perform().Pass the test and this pattern moves unknown