我想用selenium python实现简单的拖放。我正在使用 Chrome WebDriver,下面是我的代码。什么都没发生。非常感谢任何帮助。
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium import webdriver
from selenium.common import exceptions
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver import ActionChains
driver = webdriver.Chrome()
driver.get("http://html5demos.com/drag")
dragElement=driver.find_element_by_id("one")
dropElement=driver.find_element_by_id("bin")
action_chains = ActionChains(driver)
action_chains.drag_and_drop(dragElement, dropElement).perform()
Selenium 拖放功能非常简单,您需要使用 Selenium ActionChains。
ActionChains 是一种自动化低级交互的方法,例如鼠标移动、鼠标按钮操作、按键和上下文菜单交互。这对于执行更复杂的操作(例如悬停和拖放)非常有用。
参考文档:
在您的具体情况下,您需要识别操作的源和目标(图像和其中一个框),然后调用
drag_and_drop
函数。
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver = webdriver.Chrome()
driver.get("http://html5demos.com/drag")
source_element = driver.find_element_by_id('bin')
dest_element = driver.find_element_by_id('two')
ActionChains(driver).drag_and_drop(source_element, dest_element).perform()
您可以执行此脚本作为目前的快速修复(对我来说在 Chrome 和 Firefox 上都很有效)。 https://gist.github.com/druska/624501b7209a74040175#file-native_js_drag_and_drop_helper-js
我所做的只是添加 simulateDragDrop(arguments[0],arguments[1]):
function simulateDragDrop(sourceNode, destinationNode) {
var EVENT_TYPES = {
DRAG_END: 'dragend',
DRAG_START: 'dragstart',
DROP: 'drop'
}
function createCustomEvent(type) {
var event = new CustomEvent("CustomEvent")
event.initCustomEvent(type, true, true, null)
event.dataTransfer = {
data: {
},
setData: function(type, val) {
this.data[type] = val
},
getData: function(type) {
return this.data[type]
}
}
return event
}
function dispatchEvent(node, type, event) {
if (node.dispatchEvent) {
return node.dispatchEvent(event)
}
if (node.fireEvent) {
return node.fireEvent("on" + type, event)
}
}
var event = createCustomEvent(EVENT_TYPES.DRAG_START)
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_START, event)
var dropEvent = createCustomEvent(EVENT_TYPES.DROP)
dropEvent.dataTransfer = event.dataTransfer
dispatchEvent(destinationNode, EVENT_TYPES.DROP, dropEvent)
var dragEndEvent = createCustomEvent(EVENT_TYPES.DRAG_END)
dragEndEvent.dataTransfer = event.dataTransfer
dispatchEvent(sourceNode, EVENT_TYPES.DRAG_END, dragEndEvent)
}
simulateDragDrop(arguments[0], arguments[1]);
我将此文件另存为 drag_and_drop.js 文件,位于项目目录的 scripts 文件夹中。 然后我在测试中使用了下一个代码:
DRAGGABLE_SELECTOR = (By.CSS_SELECTOR, "div[draggable]")
DROPPABLE_SELECTOR = (By.CSS_SELECTOR, ".ReactCollapse--content > div > div > div > div > div > div[id]")
draggable = self.app.wd.find_element(*DRAGGABLE_SELECTOR)
droppable = self.app.wd.find_element(*DROPPABLE_SELECTOR)
f = open("scripts/drag_and_drop.js", "r")
javascript = f.read()
f.close()
wd.execute_script(javascript, draggable, droppable)
还有一件事:此脚本仅接受 CSS 选择器找到的元素(因为它是 JQuery 脚本)!
当我尝试
ActionChains(driver).drag_and_drop(drag,drop).perform()
时,它对我不起作用。它表明我选择了元素,但从未拖动它。对我有用的是这个答案 [1] 在一个单独的问题中。
您可以按照以下方式尝试一下。
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath1")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "xpath2")))
action = ActionChains(driver)
action.click_and_hold(drag).pause(2).move_to_element(drop).release(drop).perform()
这些是所需的进口:
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC