测试人社区

拖拽元素实际结果与预期结果不一致

例子的url http://sahitest.com/demo/dragDropMooTools.htm

这个例子我试了手动可以把Drage me分别拖到Item1,Item2,Item3,Item4。然后我写了下面的代码,虽然代码执行pass了,但是执行结果和手动拖拽不一样

from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


def test_action_chains_drag_and_drop():
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(3)
    driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
    element=driver.find_element(By.ID,'dragger')
    target1=driver.find_element(By.XPATH,'/html/body/div[2]')
    target2=driver.find_element(By.XPATH,'/html/body/div[3]')
    target3=driver.find_element(By.XPATH,'/html/body/div[4]')
    target4=driver.find_element(By.XPATH,'/html/body/div[5]')
    action=ActionChains(driver)
    action.drag_and_drop(element,target1)
    action.drag_and_drop(element,target2)
    action.drag_and_drop(element,target3)
    action.drag_and_drop(element,target4)
    action.perform()
    sleep(10)

初始状态

image

手动拖拽—预期结果

image

代码执行结果—实际结果

image

这个是因为actionChains 是把所有动作全部加到一个列表中,然后perform()是取出来所有的操作依次执行,无法对这个操作设置停顿的;要实现依次操作可以使用下面的方法,每次实例化一个actionChains 进行操作

def test_action_chains_drag_and_drop():
    # options = Options()
    # options.add_experimental_option ("debuggerAddress", "127.0.0.1:9222")
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(3)
    driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
    element=driver.find_element(By.ID,'dragger')
    target1=driver.find_element(By.XPATH,'/html/body/div[2]')
    target2=driver.find_element(By.XPATH,'/html/body/div[3]')
    target3=driver.find_element(By.XPATH,'/html/body/div[4]')
    target4=driver.find_element(By.XPATH,'/html/body/div[5]')
    ActionChains(driver).drag_and_drop(element,target1).perform()
    sleep(2)
    ActionChains(driver).drag_and_drop(element, target2).perform()
    sleep(2)
    ActionChains(driver).drag_and_drop(element, target3).perform()
    sleep(2)
    ActionChains(driver).drag_and_drop(element, target4).perform()
    sleep(10)
from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


def test_action_chains_drag_and_drop():
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(3)
    driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
    element=driver.find_element(By.ID,'dragger')
    target1=driver.find_element(By.XPATH,'/html/body/div[2]')
    target2=driver.find_element(By.XPATH,'/html/body/div[3]')
    target3=driver.find_element(By.XPATH,'/html/body/div[4]')
    target4=driver.find_element(By.XPATH,'/html/body/div[5]')
    action=ActionChains(driver)
    action.drag_and_drop(element,target1)
    sleep(2)
    action.drag_and_drop(element,target2)
    sleep(2)
    action.drag_and_drop(element,target3)
    sleep(2)
    action.drag_and_drop(element,target4)
    sleep(2)
    action.perform()from time import sleep

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By


def test_action_chains_drag_and_drop():
    driver=webdriver.Chrome()
    driver.maximize_window()
    driver.implicitly_wait(3)
    driver.get("http://sahitest.com/demo/dragDropMooTools.htm")
    element=driver.find_element(By.ID,'dragger')
    target1=driver.find_element(By.XPATH,'/html/body/div[2]')
    target2=driver.find_element(By.XPATH,'/html/body/div[3]')
    target3=driver.find_element(By.XPATH,'/html/body/div[4]')
    target4=driver.find_element(By.XPATH,'/html/body/div[5]')
    action=ActionChains(driver)
    action.drag_and_drop(element,target1)
    sleep(2)
    action.drag_and_drop(element,target2)
    sleep(2)
    action.drag_and_drop(element,target3)
    sleep(2)
    action.drag_and_drop(element,target4)
    sleep(2)
    action.perform()

老师,我有一版代码是这样的,只是action.perform()下没有加sleep,结果也是和实际结果一样。又是为什么?

在前面的一个回复中我已经说过了,perform()是它的内部操作;不会出现等待的,只要它的元素成功定位了,它就会进行操作
附源码:
image

明白了,谢谢老师