Why complex interactions need Actions in Selenium Python - Automation Benefits in Action
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.common.action_chains import ActionChains # Setup WebDriver (assuming chromedriver is in PATH) driver = webdriver.Chrome() try: driver.get('https://jqueryui.com/droppable/') # Switch to frame containing draggable and droppable elements 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 droppable text changed to 'Dropped!' dropped_text = WebDriverWait(driver, 10).until( EC.text_to_be_present_in_element((By.ID, 'droppable'), 'Dropped!') ) assert dropped_text, "Droppable text did not change to 'Dropped!'" finally: driver.quit()
This script automates a drag and drop interaction on a webpage using Selenium with Python.
First, it opens the webpage and switches to the frame containing the draggable and droppable elements because these elements are inside an iframe.
It waits explicitly for the draggable and droppable elements to be present to avoid timing issues.
Then, it uses ActionChains to perform the drag and drop action, which is necessary for complex interactions that simple click or send keys cannot handle.
Finally, it verifies that the droppable element's text changes to 'Dropped!' to confirm the action succeeded.
The script uses explicit waits and proper locators for reliability and maintainability.
Now add data-driven testing with 3 different draggable and droppable element pairs on the page
Practice
Actions in Selenium for some interactions?Solution
Step 1: Understand simple vs complex interactions
Simple clicks or typing can be done directly, but complex actions like drag or hover need multiple steps.Step 2: Role of
ActionsclassActionslets you build these multi-step user interactions and then perform them together.Final Answer:
Because some user actions like drag-and-drop or hover require multiple steps. -> Option DQuick Check:
Complex user steps = Use Actions [OK]
- Thinking Actions make tests faster
- Believing Actions find elements automatically
- Confusing Actions with assertion tools
Solution
Step 1: Check the correct class name and constructor
The correct class isActionChainsand it requires thedriveras a parameter.Step 2: Validate the syntax
ActionChains(driver)is the correct syntax to create an action chain object.Final Answer:
actions = ActionChains(driver) -> Option AQuick Check:
Correct class and driver passed = actions = ActionChains(driver) [OK]
- Misspelling ActionChains as ActionChain
- Not passing driver to constructor
- Calling non-existent start() method
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')Solution
Step 1: Analyze the action chain
The code moves the mouse to the element, clicks it, then performs the action chain.Step 2: Check the print statement
After performing the actions, it prints 'Action performed'.Final Answer:
The mouse moves to the element, clicks it, and prints 'Action performed'. -> Option AQuick Check:
Correct chaining and perform() call = The mouse moves to the element, clicks it, and prints 'Action performed'. [OK]
- Forgetting parentheses on perform()
- Assuming move_to_element alone triggers click
- Ignoring that element is defined before use
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()
Solution
Step 1: Check drag_and_drop method parameters
drag_and_drop requires two arguments: source and target elements.Step 2: Identify missing argument
The code only passes the source element, missing the target element.Final Answer:
Missing the target element in drag_and_drop method. -> Option BQuick Check:
drag_and_drop needs source and target [OK]
- Passing only one element to drag_and_drop
- Not creating ActionChains with driver
- Calling perform() too early
Solution
Step 1: Understand keyboard shortcut with modifier keys
Ctrl + A requires pressing Ctrl down, then 'a', then releasing Ctrl.Step 2: Use Actions to build this sequence
Usekey_down(Keys.CONTROL), thensend_keys('a'), thenkey_up(Keys.CONTROL), and finallyperform().Final Answer:
actions.key_down(Keys.CONTROL).send_keys('a').key_up(Keys.CONTROL).perform() -> Option CQuick Check:
Use key_down + send_keys + key_up + perform [OK]
- Trying to send combined keys as string
- Not releasing modifier key with key_up
- Using send_keys directly without Actions
Ready to prove it?
Pass the test and this pattern moves unknown
