Drag n Drop不会发生

时间:2017-07-11 09:06:59

标签: java selenium selenium-webdriver

不确定是什么问题..以下代码应该可以正常工作..

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class jusTrails {

    public static void main(String[] args) {
        System.setProperty("webdriver.gecko.driver", "D:\\bala back up\\personel\\selenium\\Jars\\Drivers\\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);

        driver.get("http://www.seleniumeasy.com/test/drag-and-drop-demo.html");
        WebElement onlyOne=driver.findElement(By.xpath("//span[contains(text(), 'Draggable 1')]"));

        System.out.println(onlyOne.getText());
        WebElement dropHere1=driver.findElement(By.xpath("//*[@id='mydropzone']"));
        Actions act=new Actions(driver);
        act.dragAndDrop(onlyOne, dropHere1).build().perform(); //This should work fine.. BUT.

    }

}

我没有例外,但Draggable 1根本不会移动..我用另一种方式尝试了这个但没有运气..

Actions builder = new Actions(driver);

Action dragAndDrop = builder.clickAndHold(onlyOne)
    .moveToElement(dropHere1)
    .release(dropHere1)
    .build();

dragAndDrop.perform();

1 个答案:

答案 0 :(得分:1)

这显然是一个已知的issue,并且有一个workaround。当然这是在Ruby中。

def drag_and_drop(source,target)

    js_filepath=File.dirname(__FILE__)+"/drag_and_drop_helper.js"
    js_file= File.new(js_filepath,"r")
    java_script=""

    while (line=js_file.gets)
      java_script+=line
    end

    js_file.close

    @driver.execute_script(java_script+"$('#{source}').simulateDragDrop({ 
dropTarget: '#{target}'});")

    rescue Exception => e
      puts "ERROR :" + e.to_s

  end