DragAndDropToOffset works one way but not the other

83 views
Skip to first unread message

Nef Rey

unread,
Apr 13, 2018, 3:04:25 PM4/13/18
to Selenium Users
I have a peculiar problem.  I have a method (below) that takes coordinates to drag an element to the right by X pixels using X/Y coordinates. This works fine, but when I try the same method to move the same element to the left by 500, instead it moves the element farther to the right.  As you can see from the code, when moving to the left I subtract from the X axis. and add to point.x when moving to the right.  Anyone has any ideas to why this is happening?  I'm using edge as my browser.  The same happened when I used the selenium MoveMouseTo method. 


Enter code here...public void dragElements(IWebDriver driver,  By by, testPracticeDM.MoveDirection direction, Point point)
        {
            OpenQA.Selenium.Interactions.Actions builder = new OpenQA.Selenium.Interactions.Actions(driver);
            IWebElement element = null;
            if (WaitUntilElementIsPresent(driver, by, 30))
            {
                element = driver.FindElement(by);

                switch (direction)
                {
                    case testPracticeDM.MoveDirection.Right:
                        builder.DragAndDropToOffset(element, element.Location.X + point.X, element.Location.Y).Release().Build().Perform();      //.KeyUp(global::OpenQA.Selenium.Keys.Control).Release();
                        builder.Release();
                        break;
                    case testPracticeDM.MoveDirection.Left:
                        builder.DragAndDropToOffset(element, element.Location.X - point.X, element.Location.Y).Release().Build().Perform();                   //KeyUp(global::OpenQA.Selenium.Keys.Control).Release();
                        builder.Release();
                        break;
                    case testPracticeDM.MoveDirection.Up:
                        break;
                    case testPracticeDM.MoveDirection.Down:
                        break;
                }
            }


Nef Rey

unread,
Apr 13, 2018, 5:18:29 PM4/13/18
to Selenium Users
Ok, I'm resolving my issue and I slapped myself on the head for not realizing this.  When using any of the move or drag and drop, let say to right and then left, To move right you give a positive number, but to move to the left you use a negative number.   So I changed the code below to reflect this:
builder.DragAndDropToOffset(element, -point.X, element.Location.Y).Release().Build().Perform();

Have fun you all...
Reply all
Reply to author
Forward
0 new messages