首页 > 解决方案 > 无法使用操作拖动元素

问题描述

网址 - http://www.seleniumeasy.com/test/drag-and-drop-demo.html

System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"//drivers//chrome//chromedriver.exe");
WebDriver driver=new ChromeDriver();
    driver.manage().window().maximize();
    driver.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    WebElement itemToBeDragged=driver.findElement(By.xpath(" (//h3[contains(.,'Items to Drag')]//following-sibling::span)[1]"));
WebElement 
   whereToBeDragged=driver.findElement(By.xpath("//div[@id='mydropzone']"));
    Actions action=new Actions(driver);
    Action dragAndDrop = action.clickAndHold(itemToBeDragged).moveToElement(whereToBeDragged).release(whereToBeDragged).build();
    dragAndDrop.perform();
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    driver.close();

使用上面的代码我无法拖放元素。请帮忙 !

标签: selenium

解决方案


您可以尝试使用生成器吗

实现这个方法

    public static void dragAndDrop(WebElement fromWebElement, WebElement toWebElement) {

    Actions builder = new Actions(driver);
    builder.dragAndDrop(fromWebElement, toWebElement);
}

推荐阅读