我知道已经有其他相关帖子,但没有一个给出完整的答案。下面是我正在使用的拖放代码:
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()
你能告诉我这段代码有什么问题吗?
稍后编辑: 找到了以下有效的示例:
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()
肯定是跟页面源码有关(js代码?)但不知道是什么
最佳答案
您正在尝试拖放它是正确的。但实际的网址是 : http://www.w3schools.com/html/tryit.asp?filename=tryhtml5_draganddrop
第二件事是两个 id 在一个框架内,因此您必须先*switch_to_frame*,然后再执行 ()。
关于Python Selenium 拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039414/