Site Loader
Auckland, New Zealand
In this post we are going to discuss how we can drag and drop a control in selenium. While working with some of the controls like Web parts or widgets in web applications, we may need to test them how the application behaves while trying to organize web parts by dragging and dropping them in different locations. Hence, this code snippet which we are going to discuss in this post will come very handy to achieve the operation. Here is the application UI, which we are going to code for dragging and dropping the control from one location to another

Figure 1: Drag and Drop control

Here is the code snippet
public static void DragAndDrop(WebDriver driver, WebElement srcElement, WebElement dstElement) {
	Actions action = new Actions(driver);
	action.dragAndDrop(srcElement, dstElement).perform();
}
Here is the complete explanation video of the above code Thanks for reading the post and watching the video !!! Please leave your comments and let me know if there is anything need to be improved in the post. Thanks, Karthik KK

Post Author: Karthik kk

4 Replies to “Drag and Drop UI control in Selenium”

  1. Hi Karthik

    Thanks for sharing

    I tried this in C# and I get this error:

    The IWebDriver object must implement or wrap a driver that implements IHasInputDevices.

    Any ideas how to fix?

    Thanks

    Mark

    1. I guess you are trying to do some interaction with UI element via mouse hover, this issue can be fixed if you use action class and try to bind all the operations using bind method

  2. Got it working. I missed the perform()

    You cannot move the same element twice though:
    Thread.sleep(2000);
    DragAndDrop(driver, item1, item4);
    Thread.sleep(3000);
    DragAndDrop(driver, item2, item3);
    Thread.sleep(3000);
    DragAndDrop(driver, item1, item2);

    the first two work, but the last one drags but then puts it back in same location

    any idea why? is because the DOM has changed?

    1. Its again up to you application and how developers are dealing with UI, I cannot tell unless until I have access to the app.

      Thanks,
      Karthik KK

Leave a Reply to Mark Anderson Cancel reply

Your email address will not be published. Required fields are marked *