Selenium Tutorial – Python Selenium Action Chains

In this Selenium Tutorial we are going to talk about Python Selenium Action Chains, before this we have learned that how you can work with some basic actions on Selenium Tutorial,  for example we have learned that how you can click on a button in a web page or how you can write a text on input field, how ever some times we need to perform multiple actions for example keeping the Shift button pressed and typing text for uppercase letters, or dragging and dropping mouse actions. for these actions also we can call action chains in selenium,  Selenium Action Chains are a ways provided by Selenium to automate low level interactions with website such as mouse movements, mouse button actions, key press, and context menu(right click menu) interactions. these special methods are useful for doing more complex actions like mouse over and drag and drop that are not possible by direct webdriver actions.

 

 

 

Now let’s create our example.

 

 

 

First we are going to import our required classes from selenium.

 

 

We want to use Chrome browser and after that we want to open a website.

 

 

Now we want to find our web elements in the web page, as i have already said that there are different ways that you can locate a web element in selenium, in this article we want to

 

 

You need to create the ActionsChains and after that add your driver in the ActionChains.

 

 

Now you need to do your actions, our actions are clicking on the web elements.

 

 

And at the end just perform your actions.

 

 

 

Run the code and this is the result, you can see that four elements are being clicked.

Selenium Tutorial - Python Selenium Actions Chains
Selenium Tutorial – Python Selenium Action Chains

 

 

 

 

 

Let’s create another example, in this example we want to hold CTRL key with our actions.first of all we need a web page to do our automation, we are using a page from JqueryUI website and it is selectable code, you can get the code from their or you can just create an html file and add this code, you can see that we have added name attributes for our five items in the list, because we want to select these five items, this time we want to find our web elements using name attribute,  you can use find_element_by_name() locator from selenium.

 

 

 

 

This is the Python code for the second example, it is the same as the above code, but we have just used key_down(keys.Keys.CONTROL) in here, because by clicking the items i want to hold the CTRL key.

 

 

 

Run the code and this is the result.

Selenium Tutorial - Python Selenium Actions Chains
Selenium Tutorial – Python Selenium Action Chains

 

Subscribe and Get Free Video Courses & Articles in your Email

 

Leave a Comment

Share via
Copy link
Powered by Social Snap
×