首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Selenium和Python执行拖放操作

如何使用Selenium和Python执行拖放操作
EN

Stack Overflow用户
提问于 2022-02-16 15:46:52
回答 3查看 2.1K关注 0票数 0

下面我给出了我的代码:

代码语言:javascript
运行
复制
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

browser = webdriver.Firefox()
browser.maximize_window()
browser.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
browser.implicitly_wait(5)
print(browser.title)


source_element = browser.find_element(By.XPATH, "//*[@id='DHTMLgoodies_dragableElement5']")
target_element = browser.find_element(By.XPATH,"//*[@id='box106']")
actions = ActionChains(browser)
actions.drag_and_drop(source_element,target_element).perform()

我的产出:-

代码语言:javascript
运行
复制
/home/halovivek/PycharmProjects/pythonProject/venv/bin/python /home/halovivek/PycharmProjects/pythonProject/draganddrop.py
Demo 2: Drag and drop
Traceback (most recent call last):
  File "/home/halovivek/PycharmProjects/pythonProject/draganddrop.py", line 21, in <module>
    actions.drag_and_drop(source_element,target_element).perform()
  File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/action_chains.py", line 75, in perform
    self.w3c_actions.perform()
  File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/common/actions/action_builder.py", line 77, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "/home/halovivek/PycharmProjects/pythonProject/venv/lib/python3.9/site-packages/selenium/webdriver/remote/errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined
Stacktrace:
element.getInViewCentrePoint@chrome://remote/content/marionette/element.js:1185:5
getElementCenter@chrome://remote/content/marionette/action.js:1497:22
dispatchPointerMove/<@chrome://remote/content/marionette/action.js:1378:34
dispatchPointerMove@chrome://remote/content/marionette/action.js:1374:10
toEvents/<@chrome://remote/content/marionette/action.js:1145:16
action.dispatchTickActions@chrome://remote/content/marionette/action.js:1055:35
action.dispatch/chainEvents<@chrome://remote/content/marionette/action.js:1023:20
action.dispatch@chrome://remote/content/marionette/action.js:1029:5
performActions@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:459:18
receiveMessage@chrome://remote/content/marionette/actors/MarionetteCommandsChild.jsm:144:31

Process finished with exit code 1

我无法拖放它。如何做表演动作?我试过很多种方法,但无法找到正确的解决办法。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2022-02-16 19:06:18

要实现drag_and_drop(),您需要为https://stackoverflow.com/a/54194511/7429447引入WebDriverWait,您可以使用以下任何一个https://stackoverflow.com/a/48056120/7429447

票数 1
EN

Stack Overflow用户

发布于 2022-02-16 15:56:06

这个对我有用

代码语言:javascript
运行
复制
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
drg = driver.find_element(By.XPATH, "//*[@id='dropContent']//div[@class='dragableBox' and @id='box5']")
drp = driver.find_element(By.XPATH, "//*[@id='countries']//div[@class='dragableBoxRight' and @id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)
driver.quit()

快照

票数 1
EN

Stack Overflow用户

发布于 2022-02-17 03:38:47

代码语言:javascript
运行
复制
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait

driver = webdriver.Firefox()
driver.maximize_window()
driver.get('http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html')
time.sleep(5)
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-3.html")
drag = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'box') and text()='Rome']")))
drop = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[starts-with(@id, 'box') and text()='South Korea']")))
ActionChains(driver).drag_and_drop(drag, drop).perform()


'''drg = driver.find_element(By.XPATH, "//*[@id='dropContent']//div[@class='dragableBox' and @id='box5']")
drp = driver.find_element(By.XPATH, "//*[@id='countries']//div[@class='dragableBoxRight' and @id='box105']")
ActionChains(driver).drag_and_drop(drg, drp).perform()
time.sleep(5)'''
#driver.quit()

这是修改过的代码。现在起作用了。非常感谢你的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71144948

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档