Python Selenium
About Lesson

In this Python Selenium lesson we want to learn about Selenium Mouse Based Action Chains in Python. There are around eight different mouse actions that can be performed using the Action Chains class, we have already saw some of them, like clicking on the elements, in here we want to learn about some more Mouse Based Action Chains in Python Selenium.

 

 

move_by_offset()

The move_by_offset() method is used to move the mouse from its current position to another point on the web page. Developers can specify  the X distance and Y distance the mouse has to be moved. 

So first of all we need a web page to do our automation, we are using a code from JqueryUI 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, and we want to use the find_element_by_name() locator from selenium.

 

 

And this is the Python code, you can see that first of all we have located the web element in page, in this example i want to move my mouse from the third web element by 200 in the X and 200 in Y directions. and you can see that we have used move_by_offset() and you need to give the X position and Y position.

 

 

Click

We have already saw clicking and it is used for clicking on the web elements in a web page.  in this example first we have located our three web elements by name attribute, after that we have created our ActionChains object and at the end we have used the click method for clicking of the three web elements in the web page.

 

 

 

Run the code and it will select the web elements from one up to three.

Python Selenium - Mouse Based Action Chains
Python Selenium – Mouse Based Action Chains

 

 

click_and_hold()

The click_and_hold() method is another method of the Actions class that left-clicks on an element and holds it without releasing the left button of the mouse. This method will be useful when executing operations such as drag-and-drop.

 

In the above example we want to hold the first web element and release that in the place of fourth web element, you can see that we have used click_and_hold() method, also we have used release() method for the releasing of the web elements.

 

 

Run the code and this is the result.

Python Selenium - Mouse Based Action Chains
Python Selenium – Mouse Based Action Chains

 

 

Drag and Drop in Python Selenium

Now we are going to create Drag and Drop in Python Selenium, there are two ways that you can do this, the first one is that you can use click_and_hold() method and the second way is the drag_and_drop() method.

 

So first let’s used click_and_hold() method for creating of drag and drop in Python Selenium. in this code first we have located our web elements using id, after that we have used our click_and_hold() method with release().

 

 

There might be many instances where we may have to drag-and-drop components or WebElements of a web page. now we have saw that how you can use click_and_hold() for drag and drop, but there is a method of drag_and_drop() in selenium that you can use.

 

In the above example we have used drag_and_drop() method, you need to give the source and target for drag and drop.

 

 

Run the code ad this is the result.

Python Selenium - Mouse Based Action Chains
Python Selenium – Mouse Based Action Chains

 

Thank you for visiting my website! If you enjoyed the free courses, please consider supporting my works on Patreon.
Exercise Files
No Attachment Found
No Attachment Found