Skip to content

Instantly share code, notes, and snippets.

@p0deje
Created June 29, 2022 14:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p0deje/df701a0fd05f970527ebafdc3c747088 to your computer and use it in GitHub Desktop.
Save p0deje/df701a0fd05f970527ebafdc3c747088 to your computer and use it in GitHub Desktop.
Workaround drag and drop issues in Chrome
# Take from https://github.com/bormando/selenium-tools
SNIPPET = <<~JS
function simulateDragDrop(sourceNode, destinationNode) {
var EVENT_TYPES = {
DRAG_END: 'dragend',
DRAG_START: 'dragstart',
DROP: 'drop'
}
function createCustomEvent(type) {
var event = new CustomEvent("CustomEvent")
event.initCustomEvent(type, true, true, null)
event.dataTransfer = {
data: {
},
setData: function(type, val) {
this.data[type] = val
},
getData: function(type) {
return this.data[type]
}
}
return event
}
function dispatchEvent(node, type, event) {
if (node.dispatchEvent) {
return node.dispatchEvent(event)
}
if (node.fireEvent) {
return node.fireEvent("on" + type, event)
}
}
var event = createCustomEvent(EVENT_TYPES.DRAG_START)
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event)
var dropEvent = createCustomEvent(EVENT_TYPES.DROP)
dropEvent.dataTransfer = event.dataTransfer
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent)
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END)
dragEndEvent.dataTransfer = event.dataTransfer
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent)
}
simulateDragDrop(arguments[0], arguments[1]);
JS
draggable = driver.find_element(id: 'drag1')
droppable = driver.find_element(id: 'div1')
driver.execute_script(js, draggable, droppable)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment