I am trying to drag an element into another element using Selenium WebDriver but it’s not working. I tried all the solutions which I can find on internet but none of the solutions seems to be working for me.
WebElement sourceelement = driver.findElement(By.cssSelector(“XXX”));
WebElement destelement = driver.findElement(By.cssSelector(“YYY”));
Code1:-
Actions builder = new Actions( _controls.getDriver());
builder.dragAndDrop(sourceelement, destelement);
Code2:-
Actions builder = new Actions(_controls.getDriver());
Action dragAndDrop =
builder.clickAndHold(sourceelement).moveToElement(destelement).release(destelement).build();
Thread.sleep(2000);
dragAndDrop.perform()
Code3:-
Point coordinates1 = sourceelement.getLocation();
Point coordinates2 = destelement.getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates1.getX(), coordinates1.getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(coordinates2.getX(), coordinates2.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);
Code4:-
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);”;
((JavascriptExecutor)_controls.getDriver()).executeScript(java_script, sourceelement, destelement);
Thread.sleep(2000);
None of the above code is working for me. All the above runs without any error but drag and drop is not happening in the application. Anyone having any other solution ? Thanks.
Browser :- IE
Source: Read More