Warning: fopen(/tmp/SghXTngBZPli-h6b4ru.tmp): failed to open stream: Disk quota exceeded in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 139
Warning: unlink(/tmp/SghXTngBZPli-h6b4ru.tmp): No such file or directory in /home/executea/public_html/blog/wp-admin/includes/class-wp-filesystem-ftpext.php on line 142
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”
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.
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
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
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
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
methodGot 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?
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