drag_and_drop_by_offset method – Action Chains in Selenium Python


The drag and drop by offset method is performed using the Action chain API in selenium. It is similar to simple drag and drop in addition to creating the functionality using the offset of the object. In this article, we will create the drag and drop by offset functionality using an action chain in Selenium Python.

What is the drag and drop by Offset method?

The drag and drop by offset method is an action to drag an element from one location to another using its offset position. Action chin provides a method drag_and_drop_by_offset() which takes two arguments- one is the element to be dragged and the other is x and y offset values.

The x and y offset values specify the pixels the element is to be moved in horizontal and vertical direction respectively. The offset value is a relative value to the current position of the element. For example, if the current position of the element is (x1, y1) and the offset values are (dx, dy), the new position of the element after the drag action will be (x1+dx, y1+dy).

Example

In the below example, we use the drag_and_drop_by_offset method to move a slider on the jQuery UI website. we first navigate to the jQuery UI website and switch to the iframe containing the slider element. We then locate the slider element using the find_elementmethod and create an instance of ActionChains.We then chain the drag_and_drop_by_offset action twice to move the slider handle to the right by 50 pixels and back to the left by 50 pixels, respectively.

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

# Create a new Chrome browser instance
driver = webdriver.Chrome()

# Navigate to the jQuery UI website
driver.get("https://jqueryui.com/slider/")

# Switch to the iframe containing the slider element
driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, ".demo-frame"))

# Find the slider element
slider_frame = driver.find_element(By.CSS_SELECTOR, "#slider")
slider = slider_frame.find_element(By.CSS_SELECTOR, ".ui-slider-handle")

# Create an instance of ActionChains
action_chains = ActionChains(driver)

# Chain the drag and drop action with an offset of (50, 0) pixels
# to move the slider handle to the right by 50 pixels
action_chains.drag_and_drop_by_offset(slider, 50, 0).perform()

# Chain the drag and drop action with an offset of (-50, 0) pixels
# to move the slider handle back to the left by 50 pixels
action_chains.drag_and_drop_by_offset(slider, -50, 0).perform()

# Close the browser window
driver.quit()

Output

Conclusion

In this article, we discussed the implementation of drag and drop by offset method by using an action chain in Selenium Python. The drag and drop by offset method is used to perform an action to drag an element from one location to another using its offset position.

Updated on: 10-Jul-2023

360 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements