Skip to content

Instantly share code, notes, and snippets.

@reynish
Created November 12, 2013 17:36
Show Gist options
  • Save reynish/7435208 to your computer and use it in GitHub Desktop.
Save reynish/7435208 to your computer and use it in GitHub Desktop.
Python Selenium Drag and Drop
import unittest
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
import time
baseUrl = 'http://jsbin.com/afica/13344'
class DragAndDropTest(unittest.TestCase):
def setUp(self):
# self.driver = webdriver.PhantomJS('C:\Testing\phantomjs-1.9.2\phantomjs.exe')
self.driver = webdriver.Chrome()
def test_sd_draganddrop(self):
driver = self.driver
driver.get(baseUrl)
# Now for the drag and drop!
element = driver.find_element_by_css_selector("#jackosborne")
target = driver.find_element_by_css_selector("#drop")
driver.implicitly_wait(0.5)
action_chains = ActionChains(driver)
# action_chains.move_to_element(element).click_and_hold(element).move_to_element(target).perform()
action_chains.drag_and_drop(element, target).perform()
try:
driver.find_element_by_css_selector("#drop p a")
except NoSuchElementException:
assert 0, "No a in the p"
def tearDown(self):
driver = self.driver
self.driver.close()
if __name__ == "__main__":
unittest.main()
@Jainu-s
Copy link

Jainu-s commented Sep 15, 2020

Thanks for clear coding. Can we upload an image to Facebook? I tried but it is not working. Can you help me how to handle this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment