Drag and drop is a gesture in which the user selects a virtual object by grabbing it and dragging it to a different location or into another virtual object. There is a known bug in Selenium that sometimes the drag and drop action doesn’t work (or, to be specific, the drop action doesn’t occur in this sequence).
This Medium article on “How to Bypass Selenium Drag & Drop Bug in Python” tells us how to resolve this issue.
Below Dmitrii answers some questions about the drag and drop bug and Selenium Tools package.
Q: Why is the drag and drop action broken in Selenium?
A: Well, it isn’t really broken. It’s partly broken. Drag and drop works in some cases and doesn’t in others. David Burnes, one of the core Selenium committers, said that it’s an issue for web elements that have a draggable attribute.
Q: Does this bug appear on Selenium only?
A: No, actually it’s not a Selenium bug originally, it’s a WebDriver bug. Selenium sends requests to the WebDriver API and it doesn’t respond properly on drop actions when it’s done with web elements that have the droppable attribute. It means that this bug reproduces for every framework that uses WebDriver for web browser automation.
Q: How does the drag and drop workaround work?
A: Yes, you simply install the Selenium Tools package as a dependency into your project and import it where you need to perform drag and drop action where it doesn’t work with native Selenium tools. Drag and drop function from the Selenium Tools package requires a WebDriver instance and draggable, droppable webelements to be provided. I must also add that you must use elements that were found by CSS selectors ONLY. It works only with CSS selectors since the provided script doesn’t support XPath.
Q: What Selenium Tools package can do at this moment besides drag and drop?
A: At this moment, the drag and drop workaround is the only feature that this package has. I’ve made it to help others to resolve the issue that I once had when it was critical to perform drag and drop in a test case. I’ll definitely add some other useful features if they’ll come up. Since this package is open source, so I’d appreciate any related contributions.
Q: Why would you create a separate package for drag and drop workaround for Selenium but not fix the bug itself?
A: As I’ve already mentioned, it’s a WebDriver bug, so WebDriver developers should fix it, but they haven’t for many years (more than 5 years already). I spoke with David Burnes (core Selenium committer) about pushing that workaround into Selenium, but he said that it’s not a good idea to have any workarounds in Selenium itself. That’s why I had to create a separate package to help the test automation community with this problem.
How are you tackling drag and drop scenarios? Share your experience in the comments below 😎