Action chains in Selenium Python - Build an Automation Script
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC # Setup WebDriver with webdriver.Chrome() as driver: driver.get('https://jqueryui.com/droppable/') # Switch to frame containing draggable and droppable WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, '.demo-frame'))) # Wait for draggable element draggable = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'draggable'))) droppable = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, 'droppable'))) # Perform drag and drop using ActionChains 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}'" # No need to quit explicitly because of 'with' context
This script opens the jQuery UI droppable demo page, which contains draggable and droppable elements inside an iframe.
We first wait for the iframe to be available and switch to it, because the draggable and droppable elements are inside that frame.
Then we wait explicitly for both elements to be present using WebDriverWait and expected_conditions.
We use ActionChains to drag the draggable element and drop it onto the droppable element.
After performing the drag and drop, we check the text of the droppable element to confirm it changed to 'Dropped!'. This verifies the action was successful.
The use of explicit waits ensures the elements are ready before interaction, avoiding flaky tests.
The with statement ensures the browser closes automatically after the test.
Now add data-driven testing with 3 different draggable and droppable element pairs on the page
Practice
ActionChains in Selenium with Python?Solution
Step 1: Understand the role of ActionChains
ActionChains is designed to handle complex user interactions like mouse movements, clicks, and keyboard inputs in a sequence.Step 2: Compare with other options
Locating elements, assertions, and starting the server are handled by other Selenium components, not ActionChains.Final Answer:
To perform complex mouse and keyboard actions in sequence -> Option BQuick Check:
ActionChains = complex actions [OK]
- Confusing ActionChains with element locating
- Thinking ActionChains runs tests or assertions
- Assuming ActionChains starts the WebDriver
Solution
Step 1: Check the correct import path
The correct import path isselenium.webdriver.common.action_chainsfor ActionChains.Step 2: Verify object creation syntax
ActionChains requires the WebDriver instance as an argument during creation, soActionChains(driver)is correct.Final Answer:
from selenium.webdriver.common.action_chains import ActionChains actions = ActionChains(driver) -> Option CQuick Check:
Import from common.action_chains + pass driver [OK]
- Forgetting to pass driver to ActionChains constructor
- Using wrong import path or syntax
- Calling ActionChains without parentheses
from selenium.webdriver.common.action_chains import ActionChains actions = ActionChains(driver) actions.move_to_element(element).click().perform()
Solution
Step 1: Analyze the chained actions
The code chainsmove_to_element(element)followed byclick(), then callsperform()to execute both actions.Step 2: Understand perform() role
Callingperform()runs all chained actions in order, so the mouse moves to the element and clicks it.Final Answer:
Moves mouse to the element and clicks it -> Option AQuick Check:
move_to_element + click + perform = move and click [OK]
- Forgetting to call perform() so actions don't run
- Assuming click works without moving mouse first
- Thinking perform() is optional
actions = ActionChains(driver) actions.click(element) actions.perform()
Solution
Step 1: Check click() method usage
Theclick()method can accept an element as an argument to click it directly without needing move_to_element.Step 2: Verify perform() call
Callingperform()on the ActionChains object executes all queued actions correctly.Final Answer:
No error; code will run correctly -> Option AQuick Check:
click(element) + perform() = valid usage [OK]
- Thinking move_to_element is always required before click
- Assuming perform() must be chained immediately
- Missing parentheses on click() (not the case here)
source and drop it onto target using ActionChains. Which code snippet correctly performs this action?Solution
Step 1: Understand drag_and_drop method
drag_and_drop(source, target)is a built-in ActionChains method that performs drag and drop in one call.Step 2: Understand manual drag and drop steps
Usingclick_and_hold(source), thenmove_to_element(target), thenrelease()andperform()also correctly performs drag and drop manually.Step 3: Evaluate other options
actions.move_to_element(source).click().move_to_element(target).click().perform() clicks source and target separately, which is not a drag and drop action.Final Answer:
Both B and D are correct ways to drag and drop -> Option DQuick Check:
drag_and_drop or click_hold+move+release = drag-drop [OK]
- Using click instead of click_and_hold for drag
- Missing release() after moving element
- Confusing drag_and_drop with simple clicks
Ready to prove it?
Pass the test and this pattern moves unknown
