Action Chains in Selenium Python

Vaibhhav Khetarpal Oct 10, 2023
  1. How to Create a Simple Action Chain Object in Selenium Python
  2. Implement an Action Chain in Selenium Python
Action Chains in Selenium Python

Selenium is a standalone web-based tool utilized for automation. It is an open-source tool that anyone can utilize.

When combined with the Python language, this tool is utilized for testing. Action chains are a fundamental part of Selenium and offer a way to manage low-level interactions like keypress, mouse movements, mouse button actions, and interactions with the context menu.

This tutorial demonstrates how to implement action chains in Selenium with Python.

We usually use action chains when there is a need to automate advanced scripts in which we need to either click or drag on any of the elements.

Action chain objects are utilized to implement Action Chains in Python. An action chain object can store all the actions in a queue, which performs all the stored operations by calling the perform() function.

How to Create a Simple Action Chain Object in Selenium Python

A simple Action Chain object can be created by importing the necessary modules and passing some values in Python code.

The following code creates a simple Action Chain object.

from selenium import webdriver  # webdriver is imported from selenium

from selenium.webdriver.common.action_chains import (
    ActionChains,
)  # ActionChains is imported from webdriver

driver = webdriver.Firefox()  # a webdriver object is then created

action = ActionChains(
    driver
)  # An action chain object is finally created with the driver
  1. The Action Chain class is firstly imported from the selenium.webdriver module.
  2. A driver is then defined.
  3. This driver is then passed as the key argument to the action chain object.
  4. The action chain object is created and ready to perform any feasible operation.

Apart from the generic utilization, action chains can also be used in chain or queue patterns.

Implement an Action Chain in Selenium Python

To explain this better, let us take an example of the website https://www.delftstack.com/ and do some experiments in the Python code.

The example code taken below runs the website https://www.delftstack.com/ first and then clicks on the TUTORIALS button in the header, due to which the browser then redirects us to the https://www.delftstack.com/tutorial/ link of the website on its own.

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

driver = webdriver.Firefox()
driver.get("https://www.delftstack.com/")
clicker1 = driver.find_element_by_link_text("TUTORIALS")
action = ActionChains(driver)
action.click(on_element=clicker1)
action.perform()

The above code provides the following output:

The code first runs and opens the website’s home page https://www.delftstack.com/.

actionchains in selenium python

Then, we are automatically redirected to the https://www.delftstack.com/tutorials/ webpage as per the manipulations in the above code.

actionchains in selenium python 1

  1. The Action Chain class is firstly imported from the selenium.webdriver module.
  2. A driver is then defined, which is the website https://www.delftstack.com/. We use the get() function for this.
  3. Then, we define an element clicker1, the TUTORIALS button in the website’s header.
  4. This driver is passed as the key argument to the action chain object.
  5. Then, we utilize the action.click() function and pass the previously defined clicker1 element as its argument.
  6. We then execute the perform() function so that the manipulations defined in the code can take place.

In this part of the article, we took a real-life example of using action chains in Selenium with Python.

However, just implementing an action chain in Selenium is not enough; we also need to know the Action Chain methods performed after creating an Action Chain object. We have described some of the important ones for you below.

  1. click - the method for clicking an element.
  2. click_and_hold - the method for holding the left mouse button down on the given element.
  3. double_click - the method for double-clicking an element.
  4. drag_and_drop - holds the LMB on an element, drags it to the target site, and releases it afterward.
  5. move_to_element - The mouse is moved to the element’s center.
  6. perform - all the actions stored in the action chain object are performed using this method.
  7. pause - all the inputs are paused for a given duration. The time duration is taken as a unit of seconds.
  8. release - if a mouse button is held, it releases it.
Vaibhhav Khetarpal avatar Vaibhhav Khetarpal avatar

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn

Related Article - Python Selenium