from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素
import time
driver = webdriver.Chrome()
driver.get('http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable')
wait = WebDriverWait(driver, 3)
# driver.implicitly_wait(3) # 使用隐式等待
try:
driver.switch_to.frame('iframeResult') # 切换到iframeResult
sourse = driver.find_element_by_id('draggable')
target = driver.find_element_by_id('droppable')
# 方式一:基于同一个动作链串行执行
# actions=ActionChains(driver) #拿到动作链对象
# actions.drag_and_drop(sourse,target) #把动作放到动作链中,准备串行执行
# actions.perform()
# 方式二:不同的动作链,每次移动的位移都不同
ActionChains(driver).click_and_hold(sourse).perform()
distance = target.location['x'] - sourse.location['x']
track = 0
while track < distance:
ActionChains(driver).move_by_offset(xoffset=2, yoffset=0).perform()
# ActionChains(driver).move_by_offset() # 移动x轴和y轴的距离
# ActionChains(driver).move_to_element() # 直接移动到某个控件上
# ActionChains(driver).move_to_element_with_offset() # 移动到某个控件的某个位置
# 所有的动作最后都要执行perform(),真正的去执行
track += 2
# 释放动作链
ActionChains(driver).release().perform()
time.sleep(10)
finally:
driver.close()