I am struggling to perform drag and drop or click and hold actions with Selenium in python on private web app.
I try to reproduce my mistake on a public exemple here : http://the-internet.herokuapp.com/drag_and_drop
Below is my basic piece of code for drag and drop / click and hold
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get('http://the-internet.herokuapp.com/drag_and_drop')
dragged = driver.find_element(By.ID, "column-a")
dropped = driver.find_element(By.ID, "column-b")
#Drag and drop
actions = ActionChains(driver)
actions.drag_and_drop(dragged, dropped).perform()
#column A is selected but not dragged
#Click and hold
actions = ActionChains(driver)
actions.move_to_element(dragged).click_and_hold().move_to_element(dropped).release().perform()
#Same result : column a is selected but not dragged
Searching through stackoverflow I came across a "solution" for the public exemple using javascript. How to simulate HTML5 Drag and Drop in Selenium Webdriver?
import os
with open(os.path.abspath('C:/Users/Admin/PycharmProjects/AutoTestIntro/drag_and_drop_helper.js'), 'r') as js_file:
line = js_file.readline()
script = ''
while line:
script += line
line = js_file.readline()
driver.execute_script(script+"$('#column-a').simulateDragDrop({ dropTarget: '#column-b'});")
This perfectly work directly in python but required to that dragged and dropped elements have ids. This is not the case on the private project I am currently working on.
Is there anything I am doing wrong with Selenium ? Is there any workaround in JS to specify xpath instead of id ?