拖拽功能在selenium Webdriver中不起作用

时间:2016-08-08 10:55:30

标签: java selenium selenium-webdriver

我正在努力学习硒。我有以下网站,其中拖放功能可用http://html5demos.com/drag#。我正在尝试使用以下代码进行拖放操作。但是不能做同样的事情。任何有关这方面的帮助将不胜感激。

代码1

System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\drivers\\IEDriverServer.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://html5demos.com/drag");
driver.manage().window().maximize();
List<WebElement> ele1=driver.findElements(By.id("bin"));
System.out.println(ele1.size());
System.out.println(ele1.get(0).isDisplayed());
WebElement ele2=driver.findElement(By.id("one"));
System.out.println(ele1.get(0).isDisplayed());
System.out.println(ele2.isDisplayed());
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(ele2)
.moveToElement(ele1.get(0))
.release(ele2)
.build();
dragAndDrop.perform(); 

代码2

(new Actions(driver)).dragAndDrop(ele2, ele1.get(0)).perform();

代码3

Actions builder = new Actions(driver);
builder.dragAndDropBy(ele2, -50, 35).perform();

3 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

找到了一个可以拖放HTML5的工作的JavaScript

final String java_script =
"var src=arguments[0],tgt=arguments[1];var dataTransfer={dropEffe" +
"ct:'',effectAllowed:'all',files:[],items:{},types:[],setData:fun" +
"ction(format,data){this.items[format]=data;this.types.append(for" +
"mat);},getData:function(format){return this.items[format];},clea" +
"rData:function(format){}};var emit=function(event,target){var ev" +
"t=document.createEvent('Event');evt.initEvent(event,true,false);" +
"evt.dataTransfer=dataTransfer;target.dispatchEvent(evt);};emit('" +
"dragstart',src);emit('dragenter',tgt);emit('dragover',tgt);emit(" +
"'drop',tgt);emit('dragend',src);";


driver.get("http://html5demos.com/drag");
WebElement LocatorFrom =driver.findElement(By.id("three"));
WebElement LocatorTo = driver.findElement(By.id("bin"));
((JavascriptExecutor)driver).executeScript(java_script, LocatorFrom, LocatorTo);

答案 2 :(得分:0)

试试这个:

System.setProperty("webdriver.ie.driver", System.getProperty("user.dir")+"\\drivers\\IEDriverServer.exe"); 
WebDriver driver=new ChromeDriver(); 
driver.get("http://html5demos.com/drag"); 
driver.manage().window().maximize(); 
List<WebElement> ele1=driver.findElements(By.id("bin"));
System.out.println(ele1.size()); 
System.out.println(ele1.get(0).isDisplayed()); 
WebElement ele2=driver.findElement(By.id("one"));
System.out.println(ele1.get(0).isDisplayed()); 
System.out.println(ele2.isDisplayed()); 
Actions builder = new Actions(driver); 
Action dragAndDrop = builder.clickAndHold(ele2).moveToElement(ele1.get(0)).release().build();      
dragAndDrop.perform();