Open In App

Action Chains in Selenium Python

Last Updated : 06 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Selenium’s Python Module is built to perform automated testing with Python. ActionChains are a way to automate low-level interactions such as mouse movements, mouse button actions, keypress, and context menu interactions. This is useful for doing more complex actions like hovering over and drag and drop.

Action chain methods are used by advanced scripts where we need to drag an element and click an element, This article revolves around how to manipulate DOM using Action Chains in Selenium. We have covered all the methods with examples in detail. ActionChains are implemented with the help of an action chain object which stores the actions in a queue and when perform() is called, performs the queued operations.

How to create an Action Chain Object?

To create an object of Action Chain, import the Action Chain class from docs and pass the driver as the key argument. After this one can use this object to perform all the operations of action chains.

# import webdriver
from selenium import webdriver
 
# import Action chains 
from selenium.webdriver.common.action_chains import ActionChains
 
# create webdriver object
driver = webdriver.Firefox()
 
# create action chain object
action = ActionChains(driver)

How to use Action Chains in Selenium ?

After one has created an object of Action chain, open a webpage, and perform various other methods using below syntax and examples.

Action chains can be used in a chain pattern as below:

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")

ActionChains(driver).move_to_element(menu).click(hidden_submenu).perform()


Or actions can be queued up one by one, then performed

menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav # submenu1")

actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()

Project Example

Let’s try to implement action chains using https://www.geeksforgeeks.org/ and play around with various methods of Selenium Python.

# import webdriver
from selenium import webdriver
 
# import Action chains 
from selenium.webdriver.common.action_chains import ActionChains
 
# create webdriver object
driver = webdriver.Firefox()
 
# get geeksforgeeks.org
driver.get("https://www.geeksforgeeks.org/")
 
# get element 
element = driver.find_element_by_link_text("Courses")
 
# create action chain object
action = ActionChains(driver)
 
# click the item
action.click(on_element = element)
 
# perform the operation
action.perform()


Above code, first opens https://www.geeksforgeeks.org/ and then clicks on courses button in the header, which then redirects the browser to https://practice.geeksforgeeks.org/ automatically.

Output

First driver opens https://www.geeksforgeeks.org/

action-chain-Selenium-Python


Then redirects to https://practice.geeksforgeeks.org/

click-element-method-Selenium-Python

Action Chain Methods in Selenium Python

One can perform a huge number of operations using Action chains such as click, right-click, etc. Here is a list of important methods used in Action chains.

MethodDescription
clickClicks an element.
click_and_holdHolds down the left mouse button on an element.
context_clickPerforms a context-click (right click) on an element.
double_clickDouble-clicks an element.
drag_and_dropHolds down the left mouse button on the source element, then moves to the target element and releases the mouse button.
drag_and_drop_by_offsetHolds down the left mouse button on the source element, then moves to the target offset and releases the mouse button.
key_downSends a key press only, without releasing it.
key_upReleases a modifier key.
move_by_offsetMoving the mouse to an offset from current mouse position.
move_to_elementMoving the mouse to the middle of an element.
move_to_element_with_offsetMove the mouse by an offset of the specified element, Offsets are relative to the top-left corner of the element.
performPerforms all stored actions.
pausePause all inputs for the specified duration in seconds
releaseReleasing a held mouse button on an element.
reset_actionsClears actions that are already stored locally and on the remote end
send_keysSends keys to current focused element.

Conclusion

In summary, these methods facilitate various actions in web automation, such as clicking, dragging, and key presses, allowing for comprehensive interaction and testing of web elements. They enhance automation scripts by enabling precise control over user interactions.

Frequently Asked Questions on

What is action chains in Selenium Python?

Action Chains in Selenium Python are a way to automate low-level interactions with web elements, like mouse movements, mouse button actions, and key presses.

What is the action class in Selenium Python?

In Selenium Python, the ActionChains class provides methods to automate complex interactions such as mouse movements, mouse button actions (click, double-click, right-click), key presses, and more on web elements. It allows simulating user interactions that are not possible with simple WebDriver commands.

What is action method in Selenium?

An action method in Selenium refers to a specific function within ActionChains that enables complex user interactions like mouse movements and keyboard actions on web elements.




Next Article
Practice Tags :

Similar Reads

three90RightbarBannerImg