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.