0

I have a scenario to drag a tree view item and drop it at a data item in data grid. I have tried Action class with element and action with offset and Mouse Move/Down/Up but nothing working. It clicks on the source element but not drag it to the destination element.

I have tried:

public static void DragAndDrop(this IWebElement source, IWebElement destination) 
{ 
   var destinationCenterX = destination.Location.X + destination.Size.Width / 2; 
   var destinationCenterY = destination.Location.Y + destination.Size.Height / 2; 
   var action = new Actions(driver); action.MoveToElement(source).Build().Perform();
   action.ClickAndHold(source).MoveByOffset(destinationCenterX, destinationCenterY).Build().Perform(); 
   destination.Click(); action.Release().Perform(); 
}

Or

public static void DragAndDrop(this IWebElement source, IWebElement destination) 
{ 
   Actions action0 = new Actions(driver);
action0.ClickAndHold(sourceElement).MoveToElement(destinationElement).Release().Build().Perform(); 
}

Or

public static void DragAndDrop(this IWebElement source, IWebElement destination) 
{ 
   var actions = new Actions(driver); 
   actions.DragAndDrop(sourceElement,destinationElement).Perform(); 
}

Or

public static void DragAndDrop(this IWebElement source, IWebElement destination) 
{ 
   driver.Mouse.MouseMove(sourceElement.Coordinates); 
   driver.Mouse.MouseDown(null); 
   driver.Mouse.MouseMove(destinationElement.Coordinates); 
   driver.Mouse.MouseUp(null); 
   Thread.Sleep(TimeSpan.FromSeconds(1)); 
}

I have tried different way but all the time its clicking and holding the source element but not drag it to destination element.

TLe
  • 21
  • 3
  • I'm not sure if this is your issue but, after doing the MouseDown in the source element, try moving the cursor a tiny bit within the source element before you move it to the destination element. If a human did this the cursor would move across the source item after the MouseDown and not directly to the destination element. This would capture the source element for dragging. See this answer for more information; https://stackoverflow.com/questions/55246256/drag-and-drop-in-winappdriver-doesnt-work. It's not your case exactly but I think it relates to your issue. – quaabaam Oct 24 '22 at 17:52

1 Answers1

0

Thanks @quaabaam so much. It works after I try to move the cursor a tiny bit within the source element before you move it to the destination element.

Here is the updated code that works perfectly.

        public static void DragAndDrop(WindowsElement sourceElement, WindowsElement destinationElement)
        {
            var destinationCenterX = destinationElement.Location.X + destinationElement.Size.Width / 2;
            var destinationCenterY = destinationElement.Location.Y + destinationElement.Size.Height / 2;
            var action = new Actions(DriverContext.Driver);
            action.MoveToElement(sourceElement).Build().Perform();
            action.ClickAndHold(sourceElement).MoveByOffset(10,10).MoveByOffset(destinationCenterX, destinationCenterY).Build().Perform();
            destinationElement.Click();
            action.Release().Perform();
        }
TLe
  • 21
  • 3