Skip to content

Actions drag and drop method #1365

Closed
Closed
@oflores7

Description

@oflores7

org.openqa.selenium.interactions.Actions
When using the above class from the selenium library, the method dragAndDrop(WebElement source, WebElement target) does not work. I have tried executing it multiple times with no luck. Attached is my code to use the drapAndDrop method.
DragAndDropIssue.txt

Activity

vveliev

vveliev commented on Dec 10, 2015

@vveliev

dragAndDrop

you may need to run it 2 times , 1st it will focus on the elements second time it will do drag and drop

oflores7

oflores7 commented on Dec 11, 2015

@oflores7
Author

I did what you instructed, and ran it twice but still no drag and drop action was performed.

ghost

ghost commented on Dec 22, 2015

@ghost

having the same problem here. please post the solution here once you find it.

added
B-atomsJavaScript chunks generated by Google closure
on Dec 25, 2015
tvdberk

tvdberk commented on Mar 30, 2016

@tvdberk

I've got the same issue here, unable to use the dragAndDrop method nor the clickAndHold + moveToElement + release combination

G-JShaw

G-JShaw commented on Apr 9, 2016

@G-JShaw

I'm having a similar issue using Selenium.WebDriver v2.53.0 and Firefox v45.0.1 using the C# code below. This is moving Backgrid table columns around. Was working in v2.44.0.

Anyone experiencing something similar?

var actionBuilder = new Actions(Browser);
var dragAction = actionBuilder
    .ClickAndHold(columnToDragElement)
    .MoveToElement(dragToColumnElement)
    .Release()
    .Build();
dragAction.Perform();
matt-romero00

matt-romero00 commented on Apr 19, 2016

@matt-romero00

I believe that the issue is related to HTML5. This is a solution that I found so far: http://elementalselenium.com/tips/39-drag-and-drop

lukeis

lukeis commented on Jul 20, 2016

@lukeis
Member

also this is the old original issue logged in google code:

SeleniumHQ/selenium-google-code-issue-archive#3604

8 remaining items

ffMathy

ffMathy commented on Feb 26, 2019

@ffMathy

What is the status on implementing this in Selenium?

Herst

Herst commented on Feb 26, 2019

@Herst
Contributor

@ffMathy Do you mean Drag and Drop using the HTML5 drag and drop API or Drag and Drop using a particular JS library? In any case, it's hardly something which has to do with Selenium (the WebDriver client) but rather the browsers interacted with (or in the case of HTML5 DnD the WebDriver protocol standard).

jasonex7

jasonex7 commented on Sep 25, 2019

@jasonex7

Have you tried triggering the mouseMove command twice? That seems to do the trick for me.

The same works for me on Chrome 77. Thanks!

NotEvenFlinched

NotEvenFlinched commented on Oct 29, 2019

@NotEvenFlinched

@jasonex7 @llmartinll
I tried may be all solutions discovered, including yours, but for me drag and drop works only if I move mouse during auto-test execution )
Chrome 76

llmartinll

llmartinll commented on Oct 30, 2019

@llmartinll

@Wonderio619 What are you trying to do, and what do you mean by 'auto-test execution'?

I think I remember a couple of months ago something got changed/fixed so you don't have to issue the move command twice. However, looking at my own code it seems the drag and drop by offset might stil cause problems for chrome.

Can you try if something like this works for you?

var actionBuilder = new Actions(Browser);
var dragAction = actionBuilder
    .ClickAndHold(columnToDragElement)
    .moveByOffset(10, 0)
    .moveByOffset(-10, 0)
    .moveByOffset(xOff, yOff)
    .Release()
    .Build();
dragAction.Perform();
NotEvenFlinched

NotEvenFlinched commented on Oct 30, 2019

@NotEvenFlinched

@llmartinll
I'm trying to drag n drop element on another element, and only if I physically moving mouse during drag n drop part, only then this drag n drop code works
With your code I have "move target out of bounds" exception

string selector = AttachmentPageSelector.Replace("NUMBER", pageId);
                var currentPageElement = WebBrowser.WebDriver.FindElement(By.XPath(selector));
                int xOff = LinkedPagesContainer.Element.Location.X;
                int yOff = LinkedPagesContainer.Element.Location.Y;
                var dragAction = actions.ClickAndHold(currentPageElement).MoveByOffset(10, 0).MoveByOffset(-10, 0).MoveByOffset(xOff, yOff).Release().Build();
                dragAction.Perform();

llmartinll

llmartinll commented on Oct 31, 2019

@llmartinll

You are using the destination's X,Y coordinates as an offset, that won't work. You would need the difference between the currentPageElement and the targetElement. But, I would use the .MoveToElement functionality. And maybe trigger it twice.
I think you're having 'general' issues getting your code to work. Maybe I'm wrong, but it seems more like a stackoverflow question and less like a 'selenium doesn't work as expected question'.

NotEvenFlinched

NotEvenFlinched commented on Oct 31, 2019

@NotEvenFlinched

@llmartinll
"You would need the difference between the currentPageElement and the targetElement"
Can you please advice how can I calculate or get this difference ?

Naumansh

Naumansh commented on Jun 22, 2020

@Naumansh

Is there any fix to this issue? The drag and drop does not work both on chrome and firefox. i have tried all suggested solutions but nothing works.

glundgren93

glundgren93 commented on Jun 24, 2020

@glundgren93

Any news? 😢🤞

diemol

diemol commented on Jun 26, 2020

@diemol
Member

Apologies for the late reply.

I tried to reproduce the original issue and I got the code to work. I pushed the code to this repository in case someone wants to have a look: https://github.com/diemol/selenium-issues/blob/master/selenium/issue-1365/src/test/java/DragAndDropActions.java

Here is the code running:
issue-1365

I also saw that the issue has several comments reporting other different issues. Therefore, if you are still having an issue with the most recent version of Selenium and/or the Selenium 4 Alphas, please create a new issue and provide all the required information.

locked and limited conversation to collaborators on Sep 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    B-atomsJavaScript chunks generated by Google closure

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @barancev@Mogost@lukeis@Herst@ffMathy

        Issue actions

          Actions drag and drop method · Issue #1365 · SeleniumHQ/selenium