Reputation: 11
there is a site to practice: http://the-internet.herokuapp.com/drag_and_drop
if tried to permove a simple drag and drop action in Chrome. But nothing happens (actually only one column has been selected and then nothing)
I've located elements like this
By COLUMN_A_LOCATOR = By.xpath("//*/header[contains(text(),'A')]/..");
By COLUMN_B_LOCATOR = By.xpath("//*/header[contains(text(),'B')]/..");
and tried to perform drag and drop like this
Actions actions = new Actions(driver);
actions.clickAndHold(driver.findElement(COLUMN_B_LOCATOR)).moveToElement(driver.findElement(COLUMN_A_LOCATOR)).release().perform();
and like this
actions.dragAndDrop(driver.findElement(COLUMN_B_LOCATOR),driver.findElement(COLUMN_A_LOCATOR)).perform();
and nothing. Also I've noticed that columns classNames are dynamic on this website maybe this is the reason I can't drag and drop?
also wound this https://github.com/SeleniumHQ/selenium/issues/3269 - maybe this is still a thing
Upvotes: 1
Views: 386
Reputation: 54
public void move_elements(WebElement source,WebElement target) throws InterruptedException
{
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)driver).executeScript(java_script, source, target);
}
use javascript drag and drop it will work. But I do not know why action class doesn't work on that.
Upvotes: 1