单击并拖动Selenium(chrome webdriver)未拖动,但将单击并按住

时间:2018-02-13 21:23:07

标签: java selenium testing drag

所以我试图自动化一个可以单击的列表元素,并将其拖动到ol元素的不同部分,然后保存。但是测试将尽可能地保持元素。它不会按偏移移动,也不会移动到目标元素。

Chrome webdriver,Java / Selenium

public void clickAndDragListElement() {
    Actions hold = new Actions(driver);
    hold.clickAndHold(targetHoldElement)
        .moveToElement(targetDestinationElement)
        .release(targetHoldElement)
        .build()
        .perform();
}

(Web元素在元素之外定义)

3 个答案:

答案 0 :(得分:3)

new Actions(driver)
                .moveToElement(source)
                .pause(Duration.ofSeconds(1))
                .clickAndHold(source)
                .pause(Duration.ofSeconds(1))
                .moveByOffset(1, 0)
                .moveToElement(destination)
                .moveByOffset(1, 0)
                .pause(Duration.ofSeconds(1))
                .release().perform();

答案 1 :(得分:0)

你有没有试过这样的事情:

// Create object of actions class
Actions act=new Actions(driver);

// find element which we need to drag
WebElement drag=driver.findElement(By.xpath(".//*[@id='draggable']"));

// find element which we need to drop
WebElement drop=driver.findElement(By.xpath(".//*[@id='droppable']"));

// this will drag element to destination
act.dragAndDrop(drag, drop).build().perform();

答案 2 :(得分:0)

我试过这个,它对我来说很完美:

public class DragAndDrop {

    public static void main(String[] args) {
         System.setProperty("webdriver.chrome.driver", "C:\\Users\\Ranosys\\workspace\\MyTest\\chromedriver.exe");
         WebDriver driver = new ChromeDriver();
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
          WebDriverWait wait=new WebDriverWait(driver,50 );

         driver.manage().window().maximize();
         driver.get("http://demo.guru99.com/test/drag_drop.html");
       //Element which needs to drag.           
        WebElement From=driver.findElement(By.xpath("//*[@id='credit2']/a"));   

      //Element on which need to drop.      
      WebElement To=driver.findElement(By.xpath("//*[@id='bank']/li"));                 

      //Using Action class for drag and drop.       
      Actions act=new Actions(driver);                  

    //Dragged and dropped.      
      act.dragAndDrop(From, To).build().perform();  


    }

}