Python Selenium WebDriver 拖放操作
我在使用Python的WebDriver时,拖放功能一直无法正常工作。我是在Mac OS X上使用Google Chrome和Firefox。这里有一个讨论串,里面有人遇到过类似的问题,可以看看。
我尝试过使用ActionsChains
来实现拖放:
from selenium import webdriver
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
actionChains = ActionChains(driver)
actionChains.drag_and_drop(source, target).perform()
你成功让Python WebDriver的拖放功能工作了吗?
4 个回答
3
action = ActionChains(driver)
action.click_and_hold(source).pause(4).move_to_element(target).release(target).perform()
这个功能还可以实现拖放操作。
6
目前,Action Chains 在 Mac 上无法使用。如果你在 Linux 或 Windows 上尝试上面的代码,它是可以正常工作的。ChromeDriver 快要解决这个问题了,但据我所知,仍然需要一些改进。
15
为了给出一个最新的回答,我确认这个在Mac上确实可以使用了。
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).perform()