Verify drag and drop functionality on demo page
Preconditions (2)
✅ Expected Result: The droppable element's text changes to 'Dropped!' indicating successful drag and drop
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.
Now add data-driven testing with 3 different draggable and droppable pairs on a similar page
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)
actions = ActionChains(driver) actions.click_and_hold(source).move_to_element(target).release().perform()
Pass the test and this pattern moves unknown