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.
Dmitrii Bormotov, QA Automation Lead at Flow Health and author of this article, also released Selenium Tools package for Python recently, which helps to perform bugged drag and drop in just a few lines (import + find elements + drag and drop itself) without a need to get your head deep into JavaScript and making your own bicycle.
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: Since Selenium uses WebDriver for web browser automation, we need to use something else to simulate this action. The other solution that lets us perform drag and drop is JavaScript. It’s possible to execute specific JavaScript code in a browser that will simulate such a complex action. Selenium has execute_script method in all platforms (such as Java and Python) so you can simply provide the JavaScript code that will perform drag and drop. You can also use tools built on top of Selenium such as TestProject, empowering Selenium testing with AI self healing and a vast library of automation actions, one of them being a Drag & Drop Addon.
Q: So Selenium Tools package for Python simply performs that workaround with JavaScript code?
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.
💡 You can read the details about the Selenium Tools package (seletools) here, in Python Package Index. Here’s a Gist example of how to perform drag and drop using this package.
How are you tackling drag and drop scenarios? Share your experience in the comments below 😎