下面我给出了我的代码:
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
我无法拖放它。 如何执行执行操作?我尝试了很多方法但找不到正确的解决方案。
对于
drag_and_drop()
,您需要为 element_to_be_clickable() 引入 WebDriverWait,并且您可以使用以下任一定位器策略:
使用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
浏览器快照:
这对我有用
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()
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()
当我尝试
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