Randomblue
Randomblue

Reputation: 116273

Python Selenium WebDriver drag-and-drop

I cannot get drag-and drop working with the Python WebDriver bindings. I am working with Google Chrome and Firefox on Mac OS X. There is a thread here where someone had a similar problem.

I have tried using ActionsChains:

from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
actionChains = ActionChains(driver)

actionChains.drag_and_drop(source, target).perform()

Have you managed to get the Python WebDriver drag-and-drop working?

Upvotes: 18

Views: 42323

Answers (4)

siddharth khandelwal
siddharth khandelwal

Reputation: 55

action = ActionChains(driver)
action.click_and_hold(source).pause(4).move_to_element(target).release(target).perform()

This will also do drag and drop.

Upvotes: 3

drez90
drez90

Reputation: 814

For the sake of giving an updated answer, I have verified that this does in fact work on Mac now.

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

driver = webdriver.Firefox()
driver.get("your.site.with.dragndrop.functionality.com")
source_element = driver.find_element_by_name('your element to drag')
dest_element = driver.find_element_by_name('element to drag to')
ActionChains(driver).drag_and_drop(source_element, dest_element).perform()

Reference

Upvotes: 15

AutomatedTester
AutomatedTester

Reputation: 22418

Action Chains don't currently work on Mac. If you tried the code above on Linux or Windows it would work. ChromeDriver is close to getting this right but still needs work AFAIK.

Upvotes: 6

shamp00
shamp00

Reputation: 11326

It's hard to tell exactly without some sample HTML for the source and the target.

You could try using drag_and_drop_by_offset(self, source, xoffset, yoffset) instead with a small value for the offset parameters. Sometimes that works.

You could also try to adapt this C# example which uses mouse_down_at, mouse_move_at and mouse_up_at instead.

Upvotes: 1

Related Questions