reynish
11/12/2013 - 5:36 PM

Python Selenium Drag and Drop

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()