1 from selenium import webdriver 2 from selenium.webdriver import ActionChains 3 import time 4 5 driver=webdriver.Chrome() 6 driver.implicitly_wait(10) 7 driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable') 8 9 try: 10 # 切换到id为iframeResult的窗口内 11 driver.switch_to.frame('iframeResult') 12 13 #源位置 14 draggable=driver.find_element_by_id('draggable') 15 16 #目的位置 17 droppable=driver.find_element_by_id('droppable') 18 # 调用ActionChains,必须把驱动对象进去 19 actions=ActionChains(driver) 20 21 # #方式一 22 # # 瞬间把源图片位置秒移到目标图片位置 23 # actions.drag_and_drop(draggable,droppable) 24 # #执行编辑好的行为 25 # actions.perform() 26 # time.sleep(5) 27 28 #方式二 29 source=draggable.location['x'] 30 target=droppable.location['x'] 31 print(source,target) 32 33 distance=target-source 34 print(distance) 35 36 ActionChains(driver).click_and_hold(draggable).perform() 37 s=0 38 while s<distance: 39 ActionChains(driver).move_by_offset(xoffset=2,yoffset=0).perform() 40 s+=2 41 42 ActionChains(driver).release().perform() 43 time.sleep(10) 44 45 finally: 46 driver.close()