Drag & Drop

In last section, we have discussed about the ActionChains class by which we have performed the Move to Element operation. Now we will discuss more about the Drag &  Drop Operation in Selenium Python.

Objective:

  • Introduction
  • Test Scenario
  • Drag and Drop in Selenium Python

Introduction:

As we discussed earlier in Selenium Python there is a concept called ActionChains class which will help to work with Selenium Python for Move to Element, Double click, Right Click, Drag & Drop. There are some important methods are there which are very simple and we will discuss on that in this section.

Test Scenario:

In this tutorial we will automate the JQuery website . In this you can see there are 2 box with name “Drag me to target” and “Drop here”. We will drag the “Drag me to target” and Drop to the “Drop here” block. For this we need to move our mouse pointer to the corresponding block and move the block to the target.

Drag & Drop

To automate this scenario we need a concept called ActionChains class in Selenium Python.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager


driver = webdriver.Chrome(ChromeDriverManager().install())

driver.implicitly_wait(10)
driver.get('https://jqueryui.com/droppable/')
driver.maximize_window()

src = driver.find_element(By.ID, 'draggable')
target = driver.find_element(By.ID, 'droppable')

act_chain = ActionChains(driver)
act_chain.drag_and_drop(src, target).perform()

Explanation:

From the above example you can notice that we have imported ActionChains class which helps us to drag the element from source to target element. To work with ActionChains we first we need to find the web element to which we want to move our mouse. Then we will create the the ActionChains object by providing the driver object as argument to the ActionChains().There is a direct method available for this which is known as drag_and_drop(). After we created the ActionChains object we can call the drag_and_drop() and pass source and target element to perform drag and drop. After all these, we have to call perform() function at last which is mandatory. These 3 steps will execute our this on Selenium Python.

src = driver.find_element(By.ID, 'draggable')
target = driver.find_element(By.ID, 'droppable')

act_chain = ActionChains(driver)
act_chain.drag_and_drop(src, target).perform()

This is actually a single action.

Q. For Drag  Drop what are the functions available in Selenium Python other than direct drag_and_drop() method?

. To achieve this, you need 3 separate method which will perform this operation.

  • click_and_hold
  • move_to_element
  • release()

See below example to understand in detail.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
 
driver = webdriver.Chrome(ChromeDriverManager().install())

driver.implicitly_wait(10)
driver.get('https://jqueryui.com/droppable/')
driver.maximize_window()

src = driver.find_element(By.ID, 'draggable')
target = driver.find_element(By.ID, 'droppable')

act_chain = ActionChains(driver)
act_chain.click_and_hold(src).move_to_element(target).release().perform()

Output:

Summary:

  • To work with ActionChains we first we need to find the web element to which we want to move our mouse.
  • There is a direct method available for this which is known as drag_and_drop(). After we created the ActionChains object we can call the drag_and_drop() and pass source and target element.

Now we are clear about drag and drop in selenium Python. Next section we will discuss about how to perform right click on Selenium Python.

Leave a Reply

Your email address will not be published. Required fields are marked *