简体   繁体   中英

Python Selenium Drag and Drop

I know that there are already other related posts but none of them give a complete answer. Bellow is the code for drag and drop which I'm using:

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

url = 'http://www.w3schools.com/html/html5_draganddrop.asp'
driver = webdriver.Firefox()
driver.get(url)
element = driver.find_element_by_id("drag1")
target = driver.find_element_by_id("div2")

ActionChains(driver).drag_and_drop(element, target).perform()

Can you tell me what is wrong with this code?

Later edit: Found the following example which works:

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

firefox = webdriver.Firefox()

firefox.get('http://www.theautomatedtester.co.uk/demo2.html')
draggable = firefox.find_element_by_class_name("draggable")
droppable = firefox.find_element_by_name("droppable")
dragdrop = ActionChains(firefox)\
                         .drag_and_drop(draggable, droppable)

dragdrop.perform()

It must be related to the page source (js code?) but I don't know what.

You are trying to drop and drag it's correct . But the actual url is : http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop

and the second thing is the two id's are inside a frame so you must *switch_to_frame* first before perform().

I've tried to get this working as well and it seems that switch_to_frame doesn't seem to help. Some additional research has me thinking that perhaps Selenium WebDriver doesn't fully support HTML 5 drag and drop?

https://code.google.com/p/selenium/issues/detail?id=3604

I'm going to see if I can find a nice jquery drag and drop test page that I can use test the iframe behavior on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM