如何使用 Selenium 和 Python 执行拖放操作
How to perform Drag and Drop using Selenium and Python
下面我给出了我的代码:
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()
我的输出:-
/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
我无法拖放它。
如何执行执行操作?我尝试了很多方法,但我找不到正确的解决方案。
这对我有用
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()
Snapshot
到drag_and_drop()
你需要诱导WebDriverWait for the and you can use either of the following :
使用 XPATH:
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
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()
这是修改后的代码。
现在它的工作。十分感谢你的帮助。
下面我给出了我的代码:
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()
我的输出:-
/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
我无法拖放它。 如何执行执行操作?我尝试了很多方法,但我找不到正确的解决方案。
这对我有用
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()
Snapshot
到drag_and_drop()
你需要诱导WebDriverWait for the
使用 XPATH:
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
浏览器快照:
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()
这是修改后的代码。
现在它的工作。十分感谢你的帮助。