为什么ActionChains拖放(Selenium WD)主体可与此脚本一起使用?

时间:2019-03-11 15:30:19

标签: python selenium

我基于ActionChains documentation编写了一个脚本。但是图片没有动。错误在哪里?

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

driver = webdriver.Firefox()
driver.implicitly_wait(1)
driver.maximize_window()
driver.get("https://parrot-tutorial.com/html/api_dragdrop.html")
element = driver.find_element_by_xpath("//img[@src='/images/parrot.png']")
element2 = driver.find_element_by_xpath("//div[@id='box3']")

action = ActionChains(driver)
action.move_to_element(element).click_and_hold()
action.move_to_element(element2).release()
action.perform()

time.sleep(2)
driver.quit()

1 个答案:

答案 0 :(得分:0)

Selenium提供了一种drag_and_drop方法,您尝试过吗?看起来像这样:

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

driver = webdriver.Firefox()
driver.get("your.site.with.dragndrop.functionality.com")
source_element = driver.find_element_by_name('your element to drag')
dest_element = driver.find_element_by_name('element to drag to')
ActionChains(driver).drag_and_drop(source_element, dest_element).build().perform()

请参阅文档here