在Selenium中拖放不起作用

时间:2017-05-22 10:40:46

标签: java selenium selenium-webdriver

控制台中没有显示任何错误。使用以下代码。

WebElement asd=driver.findElement(By.xpath(".//*[@id='div1']"));
    WebElement dest=driver.findElement(By.xpath(".//*[@id='drag1']"));
    Point point = dest.getLocation();
    int xcord = point.getX();
    System.out.println("Element's Position from left side Is "+xcord +" pixels.");
    int ycord = point.getY();
    System.out.println("Element's Position from top side Is "+ycord +" pixels.");
    Actions act = new Actions(driver);

            act.clickAndHold(asd);
    act.moveToElement(dest, 8, 164);
    act.perform();
    Thread.sleep(250);
    act.release(dest);
    act.perform(); 

2 个答案:

答案 0 :(得分:0)

尝试以下代码。我希望它会对你有所帮助:

        WebElement asd= = driver.findElement(By.xpath(".//*[@id='div1']"));
        WebElement dest = driver.findElement(By.xpath(".//*[@id='drag1']"));
        Actions action1 = new Actions(driver);
        action1.clickAndHold(asd).moveToElement(dest).release(asd).build().perform();

答案 1 :(得分:0)

您可以尝试以下代码:

WebElement asd= = driver.findElement(By.xpath("//*[@id='div1']"));
WebElement dest = driver.findElement(By.xpath("//*[@id='drag1']"));
Actions action1 = new Actions(driver);
action1.dragAndDrop(asd, dest).build().perform();