三种实现拖拽功能的方式
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.wait import WebDriverWait # 等待页面加载某些元素
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
ser = Service()
ser.path = r'.\chromedriver.exe'
driver = webdriver.Chrome(service=ser)
driver.get("http://www.runoob.com/try/try.php?filename=jqueryui-api-droppable")
driver.implicitly_wait(10)
driver.maximize_window()
try:
driver.switch_to.frame('iframeResult') ##切换到iframeResult
sourse = driver.find_element(By.ID, 'draggable')
target = driver.find_element(By.ID, 'droppable')
'''拿到actions对象后,对象有很多方法
1 把标签1 拖动到标签2上
actions.drag_and_drop(标签1,标签2)
2 一点点滑动某个标签
actions.click_and_hold(标签1).perform()
actions.move_by_offset(x,y) # 把标签1 滑动x轴和y轴的距离
3 滑动某个标签,一些距离
actions.drag_and_drop_by_offset(标签1,x,y)
'''
# 方式一:基于同一个动作链串行执行
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=10, yoffset=0).perform()
# track += 10
# ActionChains(driver).release().perform()
# 方式三:
# actions = ActionChains(driver)
# actions.drag_and_drop_by_offset(sourse, 200, 0).perform()
#
# time.sleep(5)
finally:
driver.close()