Reputation: 3483
I tried to implement drag and drop functionality of html elements with all following ways. But none of them work for me. This is .net framework project with latest specflow version.
1 way
var actions = new Actions(Context.WebDriver);
actions.DragAndDrop(elementDrag, destination).Release(elementDrag).Build().Perform();
2 way
actions.MoveToElement(elementDrag, 10, 10, MoveToElementOffsetOrigin.Center)
.ClickAndHold(elementDrag)
.MoveByOffset(5, 5)
.MoveToElement(destination)
.Release(elementDrag)
.Build()
.Perform();
3 way
actions.MoveToElement(elementDrag, 10, 10, MoveToElementOffsetOrigin.Center)
.ClickAndHold()
.MoveByOffset(50, -300)
.Release(elementDrag)
.Build()
.Perform();
4 way
actions.ClickAndHold(elementDrag).Build().Perform();
Thread.Sleep(3000);
actions.MoveToElement(destination).Build().Perform();
actions.Release(destination).Build().Perform();
And I sow that drag and drop is a known issue of selenium and it can solve by using javasript implementation. No idea what that is.
Can anyone suggest me a solution.
Upvotes: 1
Views: 438
Reputation: 1551
You can try this version.
Identify the source and destination.
var ele1 = Browser.FindElement(By.Xpath("//div[@class='myDragableItem"));
var ele2 = Browser.FindElement(By.Xpath("//div[@class='myDestination"));
DragAndDrop(ele1, ele2);
public static void DragAndDrop(IWebElement element1, IWebElement element2)
{
WaitForElementEnabled(element1);
WaitForElementEnabled(element2);
var builder = new Actions(driver);
var dragAndDrop = builder.ClickAndHold(element1).MoveToElement(element2).Release(element1).Build();
dragAndDrop.Perform();
}
public static void WaitForElementEnabled(IWebElement element)
{
try { _wait.Until(webDriver => element.Enabled); }
catch (StaleElementReferenceException) { if (!WaitForNotFoundElement_Enabled(element)) throw; } }
}
Upvotes: 1
Reputation: 105
You can try using JavaScriptExecutor
IWebElement source= driver.FindElement(By.Id(selector));
IWebElement destination= driver.FindElement(By.Id(selector));
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.push(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);";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript(java_script, source, destination);
Upvotes: 1