Action chains let you do complex mouse and keyboard actions step-by-step. They help test things like drag-and-drop or hover effects.
Action chains in Selenium Python
from selenium.webdriver import ActionChains actions = ActionChains(driver) actions.move_to_element(element).click().perform()
Always call perform() at the end to run the actions.
You can chain many actions before calling perform().
actions = ActionChains(driver) actions.move_to_element(button).click().perform()
actions = ActionChains(driver) actions.click_and_hold(source).move_to_element(target).release().perform()
actions = ActionChains(driver) actions.double_click(element).perform()
actions = ActionChains(driver) actions.context_click(element).perform()
This script opens a demo page, drags the blue box onto the gray box, and prints the text inside the target box after dropping. The text changes to "Dropped!" if successful.
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver import ActionChains import time # Setup driver (make sure chromedriver is in PATH) driver = webdriver.Chrome() driver.get('https://crossbrowsertesting.github.io/drag-and-drop.html') # Find source and target elements source = driver.find_element(By.ID, 'draggable') target = driver.find_element(By.ID, 'droppable') # Create action chain to drag and drop actions = ActionChains(driver) actions.click_and_hold(source).move_to_element(target).release().perform() # Wait to see result time.sleep(2) # Check if drop was successful by text change result_text = target.text print(result_text) driver.quit()
Action chains simulate real user actions step-by-step, so they are slower but more accurate than simple clicks.
Always import ActionChains from selenium.webdriver.
Use explicit waits if elements take time to appear before actions.
Action chains let you do complex mouse and keyboard actions in tests.
Chain multiple actions and call perform() to run them.
Useful for drag-and-drop, hover, double-click, and right-click tests.
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
