Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
190 views
in Technique[技术] by (71.8m points)

angularjs - Angular drag & drop with HTML5 not working through Selenium and Python

My code does not working on demo AngularJS Drag and Drop list: http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import os

driver = webdriver.Firefox()
driver.get("http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple")
time.sleep(2)
source_element = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[1]/div/div[2]/ul/li[1]')
dest_element = driver.find_element_by_xpath('/html/body/div[2]/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[2]/ul/li[3]')

actions = ActionChains(driver)
actions.drag_and_drop(source_element, dest_element)
actions.perform()
time.sleep(10)
os.system("taskkill /im firefox.exe /f")

I try many solutions, but it is not working. By my opinion, problem is in "drop" action.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The elements on the webpage http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple are elements. I took your code, made a couple of simple modifications and executed the drag_and_drop functionality through and and here are the observations:

Firefox

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    
    driver = webdriver.Firefox(executable_path=r'C:UtilityBrowserDriversgeckodriver.exe')
    driver.get("http://marceljuenemann.github.io/angular-drag-and-drop-lists/demo/#/simple")
    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[text()='Demo: Simple Lists']")))
    driver.execute_script("return arguments[0].scrollIntoView(true);", element)
    source_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@dnd-list='list']/li[normalize-space()='Item A1']")))
    dest_element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//ul[@dnd-list='list']/li[normalize-space()='Item B2']")))
    ActionChains(driver).drag_and_drop(source_element, dest_element).perform()
    
  • Observation: Element with text as Item A1 is successfully dragged but is never dropped. This issue can be reproduced using Chrome / ChromeDriver as well.

  • Browser Snapshot:

drag_n_drop


Conclusion

This is a known issue with Selenium and was discussed in details within the thread HTML5 Drag and Drop with Selenium Webdriver


Alternative

For a working solution you can follow the discussion in How to simulate HTML5 Drag and Drop in Selenium Webdriver?


Outro

You can find a detailed discussion in the chromedriver issue list HTML5 drag and drop is not working which is currently blocked by the chromium issue Drag and drop not working through chrome debug protocol


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...