Action Chains in Selenium Python

Action chains in Selenium Python enable the execution of multiple browser actions together in sequence. Selenium is a popular open-source automation testing tool used to test web applications and automate browser actions. Action chains allow you to combine multiple actions like clicks, key presses, and mouse movements into a single automated workflow.

What are Action Chains in Selenium Python?

Action chains are a sequence of actions that are performed in a specific order on a web page to test for a specific outcome. These actions can include clicking elements, entering text, scrolling, dragging and dropping objects, and keyboard interactions. These actions are essentially DOM manipulations performed through the ActionChains class.

Example Use Case

Consider a user logging into their account and navigating to a specific page. The user needs to:

  • Enter credentials in the login form

  • Click the "Login" button

  • Navigate to the desired page

These actions can be automated using action chains by initiating the required actions in the correct order and executing them sequentially.

Creating Action Chain Objects

To create an action chain object, you need to import the ActionChains class and create an instance ?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains

# Create a web driver instance
driver = webdriver.Chrome()

# Create an ActionChains object
actions = ActionChains(driver)

Basic Action Chain Example

Here's a simple example demonstrating how to use action chains to interact with a web page ?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

# Create Chrome driver instance
driver = webdriver.Chrome()

try:
    # Navigate to the webpage
    driver.get("https://www.tutorialspoint.com/")
    
    # Find the element to interact with
    element = driver.find_element(By.LINK_TEXT, "Courses")
    
    # Create ActionChains instance
    actions = ActionChains(driver)
    
    # Move to element and click
    actions.move_to_element(element).click().perform()
    
finally:
    # Close the browser
    driver.quit()

Action Chain Methods

ActionChains provides various methods for different types of interactions ?

Mouse Actions

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
actions = ActionChains(driver)

# Basic mouse actions
actions.click()              # Left click
actions.double_click()       # Double click
actions.context_click()      # Right click
actions.click_and_hold()     # Click and hold

# Move mouse to element
element = driver.find_element(By.ID, "example")
actions.move_to_element(element)

# Execute all actions
actions.perform()
driver.quit()

Drag and Drop

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
actions = ActionChains(driver)

# Find source and target elements
source = driver.find_element(By.ID, "source")
target = driver.find_element(By.ID, "target")

# Drag and drop
actions.drag_and_drop(source, target).perform()

# Or drag by offset
actions.drag_and_drop_by_offset(source, 100, 50).perform()

driver.quit()

Common Action Chain Methods

Method Description Usage
click() Left click on element Button clicks, links
double_click() Double click on element File selection, text editing
context_click() Right click on element Context menus
move_to_element() Move cursor to element Hover effects, tooltips
drag_and_drop() Drag from source to target File uploads, reordering
key_down()/key_up() Press/release keys Keyboard shortcuts
pause() Wait for specified time Timing control
perform() Execute all actions Required to run chain

Key Points

  • Always call perform() to execute the action chain

  • Actions are queued until perform() is called

  • Use reset_actions() to clear the action queue

  • Chain multiple actions before calling perform()

Conclusion

Action chains in Selenium Python provide a powerful way to automate complex user interactions. They enable you to simulate real user behavior by chaining multiple actions together and executing them in sequence. Always remember to call perform() to execute your action chain.

Updated on: 2026-03-27T01:04:21+05:30

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements